thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.

Home Page:fractal.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible incorrect pagination output from JsonApiSerializer

armandojs opened this issue · comments

I'm not sure if this is an issue or not.

Almost all examples I've seen of a paginated object represented as json
has both 'pagination' and 'links' inside the 'meta' object.

Example of expected output:

{
    "data": [
        {
            "foo": "bar"
        }
        ...
    ],
    "meta": {
        "pagination": {
            "total": 0,
            "count": 10,
            "per_page": 10,
            "current_page": 1,
            "total_pages": 2,
            "links": {
                "next": "http://myapp.test/api/nicepage?page=2"
            }
        }
    }
}

Actual output:

{
    "data": [
        {
            "foo": "bar"
        }
        ...
    ],
    "meta": {
        "pagination": {
            "total": 0,
            "count": 10,
            "per_page": 10,
            "current_page": 1,
            "total_pages": 2
        }
    }
    "links": {
        "next": "http://myapp.test/api/nicepage?page=2"
    }
}

I'm probably going to extend the JsonApiSerializer and write my own 'meta' method.
Maybe this is a new standard that I'm not aware of but just in case it's not I wanted report it.

I kind of get why the links might go outside 'meta', since it's navigation, but haven't seen an example of this structure before.

There are definitely examples of links being outside of meta out there. I know in my responses I put links in a few places depending on the context:

  1. on the object inside the data array. That way the client knows the available actions for that object. i.e. the next GET, PUT, DELETE etc.
  2. on its own in a huge result set that is paginated.
  3. in the meta object.

I could be wrong, but I think we let the end user of the library decide where they want to put the links. However Im definitely open to ideas on how to make it better for the user here.

The JSON:API spec is pretty clear about this: https://jsonapi.org/format/#fetching-pagination

Pagination links MUST appear in the links object that corresponds to a collection. To paginate the primary data, supply pagination links in the top-level links object. To paginate an included collection returned in a compound document, supply pagination links in the corresponding links object

The top level (or object specific) links object must contain the pagination links if pagination is supported. The meta could also contain urls but they are just arbitrary metadata.