Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function resolveResourceData(Request $request): array
*/
protected function resolveResourceIdentifier(Request $request): string
{
if (! is_null($resourceId = $this->id($request))) {
return $resourceId;
}

if (! $this->resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineIdFor($this);
}
Expand All @@ -68,6 +72,10 @@ protected function resolveResourceIdentifier(Request $request): string
*/
protected function resolveResourceType(Request $request): string
{
if (! is_null($resourceType = $this->type($request))) {
return $resourceType;
}

if (! $this->resource instanceof Model) {
throw ResourceIdentificationException::attemptingToDetermineTypeFor($this);
}
Expand Down Expand Up @@ -219,8 +227,10 @@ protected static function resourceTypeFromModel(Model $model): string

$morphMap = Relation::getMorphAlias($modelClassName);

return Str::of(
$morphMap !== $modelClassName ? $morphMap : class_basename($modelClassName)
)->snake()->pluralStudly();
if ($morphMap !== $modelClassName) {
return Str::of($morphMap)->pluralStudly();
}

return Str::of($modelClassName)->classBasename()->snake()->pluralStudly();
}
}
8 changes: 4 additions & 4 deletions src/Illuminate/Http/Resources/JsonApi/JsonApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,21 @@ public static function configure(?string $version = null, array $ext = [], array
/**
* Get the resource's ID.
*
* @return string
* @return string|null
*/
public function id(Request $request)
{
return $this->resolveResourceIdentifier($request);
return null;
}

/**
* Get the resource's type.
*
* @return string
* @return string|null
*/
public function type(Request $request)
{
return $this->resolveResourceType($request);
return null;
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/Http/Resources/JsonApi/JsonApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@
namespace Illuminate\Tests\Http\Resources\JsonApi;

use BadMethodCallException;
use Illuminate\Container\Container;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\ResponseFactory;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class JsonApiResourceTest extends TestCase
{
protected function setUp(): void
{
Relation::morphMap(['json-api-model' => JsonApiModel::class]);

$container = Container::setInstance(new Container);
$container->instance(ResponseFactoryContract::class, new ResponseFactory(
m::mock(ViewFactory::class),
m::mock(Redirector::class)
));
}

protected function tearDown(): void
{
JsonResource::flushState();
JsonApiResource::flushState();
Relation::morphMap([], false);

Container::setInstance(null);
m::close();
}

public function testResponseWrapperIsHardCodedToData()
Expand All @@ -37,4 +61,54 @@ public function testUnableToUnsetWrapper()

JsonApiResource::withoutWrapping();
}

public function testResourceTypeIsPickedFromMorph()
{
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);

$responseData = $this->fakeJsonApiResponseForModel($model)['data'];

$this->assertArrayHasKey('type', $responseData);
$this->assertSame('json-api-models', $responseData['type']);
}

public function testIncludedResourceDoesNotContainPrimaryKey()
{
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => '[email protected]']));

$responseData = $this->fakeJsonApiResponseForModel($model);
$this->assertArrayNotHasKey('id', $responseData['included'][0]['attributes']);
}

public function testIncludedMatchingResourceAttributesAreMerged()
{
$model = new JsonApiModel(['id' => 1, 'name' => 'User']);
$model->setRelation('manager', new JsonApiModel(['id' => 2, 'name' => 'Manager']));
$model->setRelation('deputy', new JsonApiModel(['id' => 2, 'email' => '[email protected]']));

$responseData = $this->fakeJsonApiResponseForModel($model);

$this->assertEquals([
'id' => '2',
'type' => 'json-api-models',
'attributes' => [
'name' => 'Manager',
'email' => '[email protected]',
],
], $responseData['included'][0]);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timacdonald need your input on this and how it should be affected, especially when the resource (e.g., UserResource) needs id, name, and email values to be available, but the relationship only loads partial values.


protected function fakeJsonApiResponseForModel(Model $model): array
{
return (new JsonApiResource($model))
->toResponse(new Request)
->getData(true);
}
}

class JsonApiModel extends Model
{
protected $guarded = [];
}
Loading