Skip to content

Commit

Permalink
[Bugfix] Fix validating empty field list for fields query param
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Jan 25, 2023
1 parent 7c0fefb commit 137f304
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project will be documented in this file. This project adheres to
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).

## Unreleased

### Fixed

- [#225](https://github.com/laravel-json-api/laravel/issues/225) Fix validation of empty field list for a resource type
in the `fields` query parameter, e.g. `/api/v1/employees?fields[employees]=`.

## [2.5.1] - 2023-01-23

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"laravel-json-api/encoder-neomerx": "^2.0.1",
"laravel-json-api/exceptions": "^1.1.1",
"laravel-json-api/spec": "^1.2",
"laravel-json-api/validation": "^2.1.1",
"laravel-json-api/validation": "^2.1.2",
"laravel/framework": "^8.76|^9.0"
},
"require-dev": {
Expand Down
29 changes: 29 additions & 0 deletions tests/dummy/tests/Api/V1/Posts/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,35 @@ public function testSparseFieldSets(): void
$response->assertFetchedOneExact($expected)->assertIncluded([$author]);
}

/**
* @return void
* @see https://github.com/laravel-json-api/laravel/issues/225
*/
public function testSparseFieldSetsHasEmptyFieldList(): void
{
$post = Post::factory()->create();

$expected = $this->serializer
->post($post)
->only('author', 'slug', 'synopsis', 'title')
->replace('author', ['type' => 'users', 'id' => $post->author]);

$author = $this->serializer
->user($post->author)
->withoutAttributes()
->withoutRelationships();

$response = $this
->withoutExceptionHandling()
->jsonApi('posts')
->sparseFields('posts', ['author', 'slug', 'synopsis', 'title'])
->sparseFields('users', [])
->includePaths('author')
->get(url('/api/v1/posts', $expected['id']));

$response->assertFetchedOneExact($expected)->assertIncluded([$author]);
}

public function testWithCount(): void
{
$post = Post::factory()
Expand Down

0 comments on commit 137f304

Please sign in to comment.