Skip to content

Commit 0acdae7

Browse files
committed
Merge branch 'hotfix/2.1.2'
2 parents 6290b39 + 2442cf1 commit 0acdae7

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## [2.1.2] - 2022-04-04
7+
8+
### Fixed
9+
10+
- [#175](https://github.com/laravel-json-api/laravel/issues/175) Fix page URLs missing sparse field sets.
11+
612
## [2.1.1] - 2022-04-01
713

814
### Fixed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
2828
"laravel-json-api/core": "^2.2",
29-
"laravel-json-api/eloquent": "^2.1",
29+
"laravel-json-api/eloquent": "^2.1.1",
3030
"laravel-json-api/encoder-neomerx": "^2.0",
3131
"laravel-json-api/exceptions": "^1.1",
3232
"laravel-json-api/spec": "^1.1.1",

tests/dummy/tests/Api/V1/Posts/IndexTest.php

+80-1
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,90 @@ public function testFilteredAndPaginated(): void
218218
->page(['number' => 1, 'size' => 10])
219219
->get('/api/v1/posts');
220220

221-
$response->assertFetchedMany($expected)
221+
$response
222+
->assertFetchedMany($expected)
222223
->assertMeta($meta)
223224
->assertLinks($links);
224225
}
225226

227+
public function testSparseFieldSets(): void
228+
{
229+
$posts = Post::factory()->count(3)->create();
230+
231+
$expected = $posts->map(
232+
fn(Post $post) => $this->serializer
233+
->post($post)
234+
->only('author', 'slug', 'synopsis', 'title')
235+
->replace('author', ['type' => 'users', 'id' => $post->author])
236+
);
237+
238+
$authors = $this->identifiersFor(
239+
'users', $posts->pluck('author')
240+
);
241+
242+
$response = $this
243+
->jsonApi('posts')
244+
->sparseFields('posts', ['author', 'slug', 'synopsis', 'title'])
245+
->sparseFields('users', ['name'])
246+
->includePaths('author')
247+
->get('/api/v1/posts');
248+
249+
$response
250+
->assertFetchedManyExact($expected)
251+
->assertIncluded($authors);
252+
}
253+
254+
public function testSparseFieldSetsAndPaginated(): void
255+
{
256+
$posts = Post::factory()->count(5)->create();
257+
258+
$expected = $posts->take(3)->map(
259+
fn(Post $post) => $this->serializer
260+
->post($post)
261+
->only('author', 'slug', 'synopsis', 'title')
262+
->replace('author', ['type' => 'users', 'id' => $post->author])
263+
);
264+
265+
$meta = [
266+
'currentPage' => 1,
267+
'from' => 1,
268+
'lastPage' => 2,
269+
'perPage' => 3,
270+
'to' => 3,
271+
'total' => 5,
272+
];
273+
274+
$links = [
275+
'first' => 'http://localhost/api/v1/posts?' . Arr::query([
276+
'fields' => $fields = [
277+
'posts' => 'author,slug,synopsis,title',
278+
],
279+
'include' => 'author',
280+
'page' => ['number' => 1, 'size' => 3],
281+
'sort' => '-createdAt',
282+
]),
283+
'last' => $last = 'http://localhost/api/v1/posts?' . Arr::query([
284+
'fields' => $fields,
285+
'include' => 'author',
286+
'page' => ['number' => 2, 'size' => 3],
287+
'sort' => '-createdAt',
288+
]),
289+
'next' => $last,
290+
];
291+
292+
$response = $this
293+
->jsonApi('posts')
294+
->sparseFields('posts', ['author', 'slug', 'synopsis', 'title'])
295+
->includePaths('author')
296+
->page(['number' => 1, 'size' => 3])
297+
->get('/api/v1/posts');
298+
299+
$response
300+
->assertFetchedManyExact($expected)
301+
->assertExactMeta($meta)
302+
->assertLinks($links);
303+
}
304+
226305
public function testWithCount(): void
227306
{
228307
$posts = Post::factory()

tests/dummy/tests/Api/V1/TestCase.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use App\Tests\TestCase as BaseTestCase;
2323
use Illuminate\Contracts\Routing\UrlRoutable;
24+
use Illuminate\Support\Collection;
2425
use LaravelJsonApi\Testing\MakesJsonApiRequests;
2526

2627
class TestCase extends BaseTestCase
@@ -49,7 +50,7 @@ protected function setUp(): void
4950
*/
5051
protected function identifiersFor(string $type, $modelsOrResourceIds): array
5152
{
52-
return collect($modelsOrResourceIds)->map(fn($modelOrResourceId) => [
53+
return Collection::make($modelsOrResourceIds)->map(fn($modelOrResourceId) => [
5354
'type' => $type,
5455
'id' => ($modelOrResourceId instanceof UrlRoutable) ?
5556
(string) $modelOrResourceId->getRouteKey() :

0 commit comments

Comments
 (0)