Skip to content

Commit

Permalink
[Feature] Make resource classes optional (#1)
Browse files Browse the repository at this point in the history
Makes resource classes optional by using the schema to serialize models that do not have their own specific resource class.
  • Loading branch information
lindyhopchris authored Feb 2, 2021
1 parent c9a78c8 commit 4dc32e6
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 209 deletions.
30 changes: 28 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@ All notable changes to this project will be documented in this file. This projec

## Unreleased

### Added
- [#1](https://github.com/laravel-json-api/laravel/pull/1)
Resource classes are now optional. If one is not defined, the implementation falls-back to
using the Eloquent schema to serialize a model. Eloquent schema fields now have new
`hidden` and `serializeUsing` methods to customise the serialization of models by the schema.
- Resource classes now support using conditional attributes in their `meta()` method.
- New field classes `ArrayList` and `ArrayHash` have been added, to distinguish between
PHP zero-indexed arrays that serialize to JSON arrays (`ArrayList`) and PHP associative
arrays that serialize to JSON objects (`ArrayHash`). The distinction is required because
an empty array list can be serialized to `[]` in JSON whereas an empty associative array
must be serialized to `null` in JSON.

### Changed
- **BREAKING** The JsonApiResource method signatures for the `attributes()`, `relationships()`,
`meta()`, and `links()` methods have been changed so that they receive the HTTP request as the
first (and only) parameter. This brings the implementation in line with Laravel's Eloquent
resources, which receive the request to their `toArray()` method. The slight difference is
our implementation allows the request to be `null` - this is to cover encoding resources
outside of HTTP requests, e.g. queued broadcasting. When upgrading, you will need to either
delete resource classes (as they are now optional), or update the method signatures on any
classes you are retaining.

### Fixed
- [#3](https://github.com/laravel-json-api/laravel/issues/3)
Example server registration in the published configuration file prevented developer from creating
a `v1` server after adding this package to their Laravel application.
Example server registration in the published configuration file prevented developer from creating
a `v1` server after adding this package to their Laravel application.

### Removed
- **BREAKING** The `Arr` field has been removed - use the new `ArrayList` or `ArrayHash`
fields instead.

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

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"require": {
"php": "^7.4|^8.0",
"ext-json": "*",
"laravel-json-api/core": "^1.0.0-alpha.1",
"laravel-json-api/eloquent": "^1.0.0-alpha.1",
"laravel-json-api/encoder-neomerx": "^1.0.0-alpha.1",
"laravel-json-api/core": "^1.0.0-alpha.2",
"laravel-json-api/eloquent": "^1.0.0-alpha.2",
"laravel-json-api/encoder-neomerx": "^1.0.0-alpha.2",
"laravel-json-api/exceptions": "^1.0.0-alpha.1",
"laravel-json-api/spec": "^1.0.0-alpha.1",
"laravel-json-api/validation": "^1.0.0-alpha.1",
Expand Down Expand Up @@ -65,7 +65,7 @@
]
}
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"sort-packages": true
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Requests/ResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ protected function existingAttributes(object $model): iterable
{
$resource = $this->resources()->create($model);

return $resource->attributes();
return $resource->attributes($this);
}

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

/** @var Relation $relationship */
foreach ($resource->relationships() as $relationship) {
foreach ($resource->relationships($this) as $relationship) {
if ($relationship->isValidated()) {
yield $relationship->fieldName() => $relationship->data();
}
Expand Down
7 changes: 5 additions & 2 deletions stubs/resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace {{ namespace }};

use Illuminate\Http\Request;
use LaravelJsonApi\Core\Resources\JsonApiResource;

class {{ class }} extends JsonApiResource
Expand All @@ -10,9 +11,10 @@ class {{ class }} extends JsonApiResource
/**
* Get the resource's attributes.
*
* @param Request|null $request
* @return iterable
*/
public function attributes(): iterable
public function attributes($request): iterable
{
return [
'createdAt' => $this->created_at,
Expand All @@ -23,9 +25,10 @@ class {{ class }} extends JsonApiResource
/**
* Get the resource's relationships.
*
* @param Request|null $request
* @return iterable
*/
public function relationships(): iterable
public function relationships($request): iterable
{
return [
// @TODO
Expand Down
50 changes: 0 additions & 50 deletions tests/dummy/app/JsonApi/V1/Comments/CommentResource.php

This file was deleted.

7 changes: 5 additions & 2 deletions tests/dummy/app/JsonApi/V1/Posts/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace App\JsonApi\V1\Posts;

use Illuminate\Http\Request;
use LaravelJsonApi\Core\Resources\JsonApiResource;

class PostResource extends JsonApiResource
Expand All @@ -27,9 +28,10 @@ class PostResource extends JsonApiResource
/**
* Get the resource's attributes.
*
* @param Request|null $request
* @return iterable
*/
public function attributes(): iterable
public function attributes($request): iterable
{
return [
'content' => $this->content,
Expand All @@ -45,9 +47,10 @@ public function attributes(): iterable
/**
* Get the resource's relationships.
*
* @param Request|null $request
* @return iterable
*/
public function relationships(): iterable
public function relationships($request): iterable
{
return [
$this->relation('author')->showDataIfLoaded(),
Expand Down
50 changes: 0 additions & 50 deletions tests/dummy/app/JsonApi/V1/Tags/TagResource.php

This file was deleted.

47 changes: 0 additions & 47 deletions tests/dummy/app/JsonApi/V1/Users/UserResource.php

This file was deleted.

50 changes: 0 additions & 50 deletions tests/dummy/app/JsonApi/V1/Videos/VideoResource.php

This file was deleted.

2 changes: 2 additions & 0 deletions tests/dummy/tests/Api/V1/Videos/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function test(): void
->actingAs($user = $video->owner)
->jsonApi('videos')
->withData($data)
->includePaths('tags')
->post('/api/v1/videos');

$id = $response
Expand Down Expand Up @@ -98,6 +99,7 @@ public function testClientId(): void
->actingAs($video->owner)
->jsonApi('videos')
->withData($data)
->includePaths('tags')
->post('/api/v1/videos');

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

0 comments on commit 4dc32e6

Please sign in to comment.