nilportugues / php-json-api

JSON API transformer outputting valid (PSR-7) API Responses.
http://nilportugues.com
MIT License
71 stars 35 forks source link

Pagination: Set default page size to 10 to avoid inconsistent defaults #47

Closed KarimGeiger closed 8 years ago

KarimGeiger commented 8 years ago

Since the default value for items per page is 10, this value has to be returned when creating a new Page object, instead of null.

This addresses the bug that the (Laravel 5 based) API will return 15 items in the 'data' array (instead of the desired 10), but supply 10 as the meta-size. When supplying the 10 via GET, e.g. "/employees?page[size]=10", everything is correct. Otherwise, the above described will create a response like that:

GET /employees
{
"data": [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}], // 15 objects
"included": [],
"links": {
"self": {"href": "/employees?page[number]=1&page[size]=10"},
"first": {"href": "/employees?page[number]=1&page[size]=10"},
"next": {"href": "/employees?page[number]=2&page[size]=10"},
"last": {"href": "/employees?page[number]=2&page[size]=10"}
},
"meta": {
"page": {
"total": 17,
"last": 2,
"number": 1,
"size": 10
}
},
"jsonapi": {"version": "1.0"}
}
nilportugues commented 8 years ago

You're absolutely right. Thanks! :+1: :smiley:

nilportugues commented 8 years ago

@KarimGeiger rollbacked your PR. Solution over the laravel package.

Thanks :)

KarimGeiger commented 8 years ago

@nilportugues you're welcome :)