Skip to content

Commit 4dc32e6

Browse files
[Feature] Make resource classes optional (#1)
Makes resource classes optional by using the schema to serialize models that do not have their own specific resource class.
1 parent c9a78c8 commit 4dc32e6

File tree

10 files changed

+46
-209
lines changed

10 files changed

+46
-209
lines changed

CHANGELOG.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,36 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
### Added
9+
- [#1](https://github.com/laravel-json-api/laravel/pull/1)
10+
Resource classes are now optional. If one is not defined, the implementation falls-back to
11+
using the Eloquent schema to serialize a model. Eloquent schema fields now have new
12+
`hidden` and `serializeUsing` methods to customise the serialization of models by the schema.
13+
- Resource classes now support using conditional attributes in their `meta()` method.
14+
- New field classes `ArrayList` and `ArrayHash` have been added, to distinguish between
15+
PHP zero-indexed arrays that serialize to JSON arrays (`ArrayList`) and PHP associative
16+
arrays that serialize to JSON objects (`ArrayHash`). The distinction is required because
17+
an empty array list can be serialized to `[]` in JSON whereas an empty associative array
18+
must be serialized to `null` in JSON.
19+
20+
### Changed
21+
- **BREAKING** The JsonApiResource method signatures for the `attributes()`, `relationships()`,
22+
`meta()`, and `links()` methods have been changed so that they receive the HTTP request as the
23+
first (and only) parameter. This brings the implementation in line with Laravel's Eloquent
24+
resources, which receive the request to their `toArray()` method. The slight difference is
25+
our implementation allows the request to be `null` - this is to cover encoding resources
26+
outside of HTTP requests, e.g. queued broadcasting. When upgrading, you will need to either
27+
delete resource classes (as they are now optional), or update the method signatures on any
28+
classes you are retaining.
29+
830
### Fixed
931
- [#3](https://github.com/laravel-json-api/laravel/issues/3)
10-
Example server registration in the published configuration file prevented developer from creating
11-
a `v1` server after adding this package to their Laravel application.
32+
Example server registration in the published configuration file prevented developer from creating
33+
a `v1` server after adding this package to their Laravel application.
34+
35+
### Removed
36+
- **BREAKING** The `Arr` field has been removed - use the new `ArrayList` or `ArrayHash`
37+
fields instead.
1238

1339
## [1.0.0-alpha.1] - 2021-01-25
1440

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"require": {
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
28-
"laravel-json-api/core": "^1.0.0-alpha.1",
29-
"laravel-json-api/eloquent": "^1.0.0-alpha.1",
30-
"laravel-json-api/encoder-neomerx": "^1.0.0-alpha.1",
28+
"laravel-json-api/core": "^1.0.0-alpha.2",
29+
"laravel-json-api/eloquent": "^1.0.0-alpha.2",
30+
"laravel-json-api/encoder-neomerx": "^1.0.0-alpha.2",
3131
"laravel-json-api/exceptions": "^1.0.0-alpha.1",
3232
"laravel-json-api/spec": "^1.0.0-alpha.1",
3333
"laravel-json-api/validation": "^1.0.0-alpha.1",
@@ -65,7 +65,7 @@
6565
]
6666
}
6767
},
68-
"minimum-stability": "stable",
68+
"minimum-stability": "dev",
6969
"prefer-stable": true,
7070
"config": {
7171
"sort-packages": true

src/Http/Requests/ResourceRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ protected function existingAttributes(object $model): iterable
449449
{
450450
$resource = $this->resources()->create($model);
451451

452-
return $resource->attributes();
452+
return $resource->attributes($this);
453453
}
454454

455455
/**
@@ -463,7 +463,7 @@ protected function existingRelationships(object $model): iterable
463463
$resource = $this->resources()->create($model);
464464

465465
/** @var Relation $relationship */
466-
foreach ($resource->relationships() as $relationship) {
466+
foreach ($resource->relationships($this) as $relationship) {
467467
if ($relationship->isValidated()) {
468468
yield $relationship->fieldName() => $relationship->data();
469469
}

stubs/resource.stub

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace {{ namespace }};
44

5+
use Illuminate\Http\Request;
56
use LaravelJsonApi\Core\Resources\JsonApiResource;
67

78
class {{ class }} extends JsonApiResource
@@ -10,9 +11,10 @@ class {{ class }} extends JsonApiResource
1011
/**
1112
* Get the resource's attributes.
1213
*
14+
* @param Request|null $request
1315
* @return iterable
1416
*/
15-
public function attributes(): iterable
17+
public function attributes($request): iterable
1618
{
1719
return [
1820
'createdAt' => $this->created_at,
@@ -23,9 +25,10 @@ class {{ class }} extends JsonApiResource
2325
/**
2426
* Get the resource's relationships.
2527
*
28+
* @param Request|null $request
2629
* @return iterable
2730
*/
28-
public function relationships(): iterable
31+
public function relationships($request): iterable
2932
{
3033
return [
3134
// @TODO

tests/dummy/app/JsonApi/V1/Comments/CommentResource.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Posts/PostResource.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace App\JsonApi\V1\Posts;
2121

22+
use Illuminate\Http\Request;
2223
use LaravelJsonApi\Core\Resources\JsonApiResource;
2324

2425
class PostResource extends JsonApiResource
@@ -27,9 +28,10 @@ class PostResource extends JsonApiResource
2728
/**
2829
* Get the resource's attributes.
2930
*
31+
* @param Request|null $request
3032
* @return iterable
3133
*/
32-
public function attributes(): iterable
34+
public function attributes($request): iterable
3335
{
3436
return [
3537
'content' => $this->content,
@@ -45,9 +47,10 @@ public function attributes(): iterable
4547
/**
4648
* Get the resource's relationships.
4749
*
50+
* @param Request|null $request
4851
* @return iterable
4952
*/
50-
public function relationships(): iterable
53+
public function relationships($request): iterable
5154
{
5255
return [
5356
$this->relation('author')->showDataIfLoaded(),

tests/dummy/app/JsonApi/V1/Tags/TagResource.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Users/UserResource.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/dummy/app/JsonApi/V1/Videos/VideoResource.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/dummy/tests/Api/V1/Videos/CreateTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function test(): void
5656
->actingAs($user = $video->owner)
5757
->jsonApi('videos')
5858
->withData($data)
59+
->includePaths('tags')
5960
->post('/api/v1/videos');
6061

6162
$id = $response
@@ -98,6 +99,7 @@ public function testClientId(): void
9899
->actingAs($video->owner)
99100
->jsonApi('videos')
100101
->withData($data)
102+
->includePaths('tags')
101103
->post('/api/v1/videos');
102104

103105
$response->assertCreatedWithClientId(url('/api/v1/videos'), $expected);

0 commit comments

Comments
 (0)