-
Notifications
You must be signed in to change notification settings - Fork 11.8k
[12.x] JSON:API Resource #57571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,418
−9
Merged
[12.x] JSON:API Resource #57571
Changes from all commits
Commits
Show all changes
97 commits
Select commit
Hold shift + click to select a range
fce2a2e
wip
crynobone a00cd30
wip
crynobone 6457e72
Apply fixes from StyleCI
StyleCIBot 5d15ab7
wip
crynobone 2214970
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynobone 6116f06
Apply fixes from StyleCI
StyleCIBot c703cf8
wip
crynobone b219e6d
wip
crynobone 8d2da32
Apply fixes from StyleCI
StyleCIBot e7de423
wip
crynobone 882ed31
Apply fixes from StyleCI
StyleCIBot 3d75b4c
wip
crynobone 4aa295d
wip
crynobone d945d9f
Apply fixes from StyleCI
StyleCIBot c2d7dfc
wip
crynobone 50c0193
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynobone 2b69984
wip
crynobone b006401
Apply fixes from StyleCI
StyleCIBot 174b4a8
wip
crynobone 13fedeb
Apply fixes from StyleCI
StyleCIBot 5c15920
wip
crynobone 0319cbe
wip
crynobone a3ee8f7
wip
crynobone b610d9c
wip
crynobone b1c0b2a
Apply fixes from StyleCI
StyleCIBot e7a8d6e
wip
crynobone f153b5e
Apply fixes from StyleCI
StyleCIBot dab4d40
wip
crynobone c742be0
wip
crynobone 1fc4aaa
Merge remote-tracking branch 'origin/json-api-resource' into json-api…
crynobone bc8e87a
wip
crynobone 99bf9a6
wip
crynobone f9bfd62
wip
crynobone 62c56c0
Apply fixes from StyleCI
StyleCIBot e9c68b0
wip
crynobone f0f58d9
wip
crynobone e1212fb
Apply fixes from StyleCI
StyleCIBot 1928b74
wip
crynobone 4e27c4b
wip
crynobone 387e2a4
wip
crynobone 73c243a
wip
crynobone ab05c9f
wip
crynobone d8ecdbc
wip
crynobone 9ce5b07
Apply fixes from StyleCI
StyleCIBot e1f0bbd
wip
crynobone 5b3f81c
wip
crynobone eda7c76
wip
crynobone 9b279bb
Apply suggestions from code review
crynobone 6275377
wip
crynobone 280c18c
wip
crynobone ac0ec25
wip
crynobone f466566
Apply fixes from StyleCI
StyleCIBot d45e57b
wip
crynobone dc0ec65
wip
crynobone f4bf030
wip
crynobone e5c2f88
Apply fixes from StyleCI
StyleCIBot 4a8cfc2
wip
crynobone 8f9d151
Apply fixes from StyleCI
StyleCIBot 76cc84e
wip
crynobone 86884a9
Apply fixes from StyleCI
StyleCIBot afb51d1
wip
crynobone 5f52c26
Apply fixes from StyleCI
StyleCIBot 4284619
wip
crynobone 504193d
Apply fixes from StyleCI
StyleCIBot 1a51b54
wip
crynobone 0ed469a
wip
crynobone a17ac8d
remove asJsonApi from base resource
taylorotwell 6836018
update exception
taylorotwell 5459428
formatting
taylorotwell 4a5e360
fix test
taylorotwell 215b9ba
formatting
taylorotwell 573a57a
formatting
taylorotwell 115ed94
formatting
taylorotwell 1da745b
rename trait
taylorotwell 8e20f20
Merge remote-tracking branch 'origin/12.x' into json-api-resource
crynobone 4abf43a
[JSON:API] Add `toAttributes()` method (#57603)
crynobone 4451594
[JSON:API] Allows `JsonApiResource::$attributes` (#57638)
crynobone be536df
[JSON:API] Supports `JsonApiResource::toRelationships()` (#57646)
crynobone ea1ea07
formatting
taylorotwell 69086d4
Merge branch '12.x' into json-api-resource
crynobone 941c62c
Merge branch '12.x' into json-api-resource
crynobone aebb48b
[JSON:API] Usage Improvements (#57960)
crynobone 227a563
Merge branch '12.x' into json-api-resource
crynobone 9921225
Fix redundant included relationship
crynobone c8eed0b
Apply fixes from StyleCI
StyleCIBot c0174b6
Fix sparse fieldsets not filtered on included relationship
crynobone cb41bdc
Apply fixes from StyleCI
StyleCIBot 397c25b
Fix many-to-many included
crynobone f5b87e8
Automatically resolve resource type from class
crynobone ac63949
wip
crynobone ba14f47
improves tests
crynobone 5007d7d
wip
crynobone 4104858
wip
crynobone 0ad6501
wip
crynobone 47efeb8
[JSON:API] Fix recursive relationship (#58066)
crynobone a998541
formatting
taylorotwell 91ec138
[JSON:API] Allows to configure maximum nested relationships (#58082)
crynobone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Illuminate/Foundation/Console/stubs/resource-json-api.stub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace {{ namespace }}; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\Resources\JsonApi\JsonApiResource; | ||
|
|
||
| class {{ class }} extends JsonApiResource | ||
| { | ||
| /** | ||
| * The resource's attributes. | ||
| */ | ||
| public $attributes = [ | ||
| // ... | ||
| ]; | ||
|
|
||
| /** | ||
| * The resource's relationships. | ||
| */ | ||
| public $relationships = [ | ||
| // ... | ||
| ]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Illuminate/Http/Resources/JsonApi/AnonymousResourceCollection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| namespace Illuminate\Http\Resources\JsonApi; | ||
|
|
||
| use Illuminate\Container\Container; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Request; | ||
| use Illuminate\Support\Arr; | ||
|
|
||
| class AnonymousResourceCollection extends \Illuminate\Http\Resources\Json\AnonymousResourceCollection | ||
| { | ||
| use Concerns\ResolvesJsonApiRequest; | ||
|
|
||
| /** | ||
| * Get any additional data that should be returned with the resource array. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @return array | ||
| */ | ||
| #[\Override] | ||
| public function with($request) | ||
| { | ||
| return array_filter([ | ||
| 'included' => $this->collection | ||
| ->map(fn ($resource) => $resource->resolveIncludedResourceObjects($request)) | ||
| ->flatten(depth: 1) | ||
| ->uniqueStrict('_uniqueKey') | ||
| ->map(fn ($included) => Arr::except($included, ['_uniqueKey'])) | ||
| ->values() | ||
| ->all(), | ||
| ...($implementation = JsonApiResource::$jsonApiInformation) | ||
| ? ['jsonapi' => $implementation] | ||
| : [], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Transform the resource into a JSON array. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @return array | ||
| */ | ||
| #[\Override] | ||
| public function toAttributes(Request $request) | ||
| { | ||
| return $this->collection | ||
| ->map(fn ($resource) => $resource->resolveResourceData($request)) | ||
| ->all(); | ||
| } | ||
|
|
||
| /** | ||
| * Customize the outgoing response for the resource. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @param \Illuminate\Http\JsonResponse $response | ||
| * @return void | ||
| */ | ||
| #[\Override] | ||
| public function withResponse(Request $request, JsonResponse $response): void | ||
| { | ||
| $response->header('Content-Type', 'application/vnd.api+json'); | ||
| } | ||
|
|
||
| /** | ||
| * Create an HTTP response that represents the object. | ||
| * | ||
| * @param \Illuminate\Http\Request $request | ||
| * @return \Illuminate\Http\JsonResponse | ||
| */ | ||
| #[\Override] | ||
| public function toResponse($request) | ||
| { | ||
| return parent::toResponse($this->resolveJsonApiRequestFrom($request)); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the HTTP request instance from container. | ||
| * | ||
| * @return \Illuminate\Http\Resources\JsonApi\SparseRequest | ||
| */ | ||
| #[\Override] | ||
| protected function resolveRequestFromContainer() | ||
| { | ||
| return $this->resolveJsonApiRequestFrom(Container::getInstance()->make('request')); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it caused a breaking change in our tests. Rather than converting these to arrays, it's returning the [JsonResourceCollection].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a new issue with reproducing repository
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#58300 done