Skip to content

Commit c2456b3

Browse files
committed
[Bugfix] Ensure page objects have sparse fields in URLs
Previously sparse fields were not retained by the JsonApiBuilder class. This meant any page objects did not have access to the sparse field sets when generating page URLs.
1 parent bfa4f68 commit c2456b3

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
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+
## Unreleased
7+
8+
### Fixed
9+
10+
- Pass sparse field sets to the `JsonApiBuilder` class, ensuring that they are present on any generated page objects.
11+
Previously this omission meant that page URLs were missing any fields sent by the client.
12+
613
## [2.1.0] - 2022-02-20
714

815
### Added

src/QueryBuilder/JsonApiBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function withQueryParameters(QueryParametersContract $query): self
146146

147147
$this->filter($query->filter())
148148
->sortWithDefault($query->sortFields())
149+
->sparseFieldSets($query->sparseFieldSets())
149150
->with($query->includePaths())
150151
->withCount($query->countable());
151152

@@ -169,6 +170,20 @@ public function filter($filters): self
169170
return $this;
170171
}
171172

173+
/**
174+
* Set the sparse field sets for the query.
175+
*
176+
* @param mixed $fields
177+
* @return $this
178+
* @todo in a future version, these need to be used to determine which columns to retrieve from the db.
179+
*/
180+
public function sparseFieldSets($fields): self
181+
{
182+
$this->parameters->setSparseFieldSets($fields);
183+
184+
return $this;
185+
}
186+
172187
/**
173188
* Sort models using JSON:API sort fields.
174189
*

tests/lib/Acceptance/Pagination/PagePaginationTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Illuminate\Pagination\AbstractPaginator;
2929
use Illuminate\Support\LazyCollection;
3030
use LaravelJsonApi\Contracts\Pagination\Page;
31+
use LaravelJsonApi\Core\Query\QueryParameters;
3132
use LaravelJsonApi\Core\Support\Arr;
3233
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
3334
use LaravelJsonApi\Eloquent\Tests\Acceptance\TestCase;
@@ -644,6 +645,53 @@ public function testItCanRemoveMeta(): void
644645
$this->assertPage($posts->take(3), $page);
645646
}
646647

648+
public function testUrlsIncludeOtherQueryParameters(): void
649+
{
650+
$posts = Post::factory()->count(6)->create();
651+
$slugs = $posts->take(4)->pluck('slug')->implode(',');
652+
653+
$links = [
654+
'first' => [
655+
'href' => 'http://localhost/api/v1/posts?' . Arr::query([
656+
'fields' => $fields = [
657+
'posts' => 'author,slug,title',
658+
'users' => 'name',
659+
],
660+
'filter' => ['slugs' => $slugs],
661+
'include' => 'author',
662+
'page' => ['number' => '1', 'size' => '3'],
663+
'sort' => '-createdAt',
664+
]),
665+
],
666+
'last' => [
667+
'href' => $last = 'http://localhost/api/v1/posts?' . Arr::query([
668+
'fields' => $fields,
669+
'filter' => ['slugs' => $slugs],
670+
'include' => 'author',
671+
'page' => ['number' => '2', 'size' => '3'],
672+
'sort' => '-createdAt',
673+
]),
674+
],
675+
'next' => [
676+
'href' => $last,
677+
],
678+
];
679+
680+
$query = QueryParameters::make()
681+
->setFilters(['slugs' => $slugs])
682+
->setSparseFieldSets($fields)
683+
->setIncludePaths('author')
684+
->setSortFields('-createdAt');
685+
686+
$page = $this->posts
687+
->repository()
688+
->queryAll()
689+
->withQuery($query)
690+
->paginate(['size' => 3]);
691+
692+
$this->assertSame($links, $page->links()->toArray());
693+
}
694+
647695
/**
648696
* Assert that the pages match.
649697
*

0 commit comments

Comments
 (0)