laravel icon indicating copy to clipboard operation
laravel copied to clipboard

Cursor pagination does not support hashids

Open othneildrew opened this issue 3 years ago • 4 comments

Having setup and followed the docs to enable hashids, I've discovered that when using hashids with Cursor Pagination, the meta page info uses the ids from the DB column.

5a1b926235b7a9562e1aa6a727700148

_Trying without validating page query. I get "the selected page.after is invalid with proper validation _ c47f971f459a39b725c499e1561d6ece

f7e350b4cc839ba98b98fc2ce61f883b

othneildrew avatar Jun 25 '22 12:06 othneildrew

Thanks for reporting this - it is indeed a bug. There's already a PR in the cursor pagination package relating to this, and my comment on it here explains that I need to amend that package so the ID field is injected into the cursor paginator.

I'll prioritise this for a fix, probably in the next week or two.

lindyhopchris avatar Jun 25 '22 15:06 lindyhopchris

@othneildrew I think I've solved this but it would be good if you could give it a go.

To give it a go, run the following:

composer require "laravel-json-api/cursor-pagination:dev-feature/id-decoding"

To use in your schema, you would just need to do the following:

public function pagination(): CursorPagination
{
    return CursorPagination::make($this->id());
}

lindyhopchris avatar Jul 04 '22 19:07 lindyhopchris

@lindyhopchris My apologies that this took me soon long to review. But I'm happy to report that it works wonderfully, thanks so much for fixing this!

The only missing piece I think is the validation of page params now.

For example, something like this would no longer pass validation checks:

  'page.after' => 'filled|string|exists:lessons,id',
  'page.before' => 'filled|string|exists:lessons,id',

othneildrew avatar Jul 21 '22 22:07 othneildrew

Yeah good point, I need to add some Rule objects that cover validating JSON:API identifiers in query parameters (JSON:API identifiers in JSON:API documents are already validated automatically).

lindyhopchris avatar Jul 28 '22 14:07 lindyhopchris

Closing in favour of #237 - that documents that I need to add things to make validating resource IDs a lot easier.

lindyhopchris avatar Apr 22 '23 16:04 lindyhopchris