Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/Laravel/ApiPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ public function register(): void
$this->app->singleton(JsonApiItemNormalizer::class, static function (Application $app) {
$config = $app['config'];
$defaultContext = $config->get('api-platform.serializer', []);
$useIriAsId = (bool) $config->get('api-platform.jsonapi.use_iri_as_id', true);

return new JsonApiItemNormalizer(
$app->make(PropertyNameCollectionFactoryInterface::class),
Expand All @@ -989,7 +990,10 @@ public function register(): void
$defaultContext,
$app->make(ResourceMetadataCollectionFactoryInterface::class),
$app->make(ResourceAccessCheckerInterface::class),
// $app->make(TagCollectorInterface::class),
tagCollector: null,
operationResourceResolver: $app->make(OperationResourceClassResolverInterface::class),
identifiersExtractor: $app->make(IdentifiersExtractorInterface::class),
useIriAsId: $useIriAsId,
);
});

Expand Down
70 changes: 70 additions & 0 deletions src/Laravel/Tests/JsonApiUseIriAsIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;
use Workbench\App\Models\Book;
use Workbench\Database\Factories\AuthorFactory;
use Workbench\Database\Factories\BookFactory;

class JsonApiUseIriAsIdTest extends TestCase
{
use ApiTestAssertionsTrait;
use RefreshDatabase;
use WithWorkbench;

/**
* @param Application $app
*/
protected function defineEnvironment($app): void
{
tap($app['config'], static function (Repository $config): void {
$config->set('api-platform.formats', ['jsonapi' => ['application/vnd.api+json']]);
$config->set('api-platform.docs_formats', ['jsonapi' => ['application/vnd.api+json']]);
$config->set('api-platform.resources', [app_path('Models'), app_path('ApiResource')]);
$config->set('api-platform.jsonapi', ['use_iri_as_id' => false]);
$config->set('api-platform.defaults', [
'route_prefix' => '/api',
]);

$config->set('app.debug', true);
});
}

public function testGetBookUsesScalarIdAndLinksSelf(): void
{
BookFactory::new()->has(AuthorFactory::new())->create();
$book = Book::first();
$iri = $this->getIriFromResource($book);
$response = $this->get($iri, ['accept' => ['application/vnd.api+json']]);
$response->assertStatus(200);
$response->assertHeader('content-type', 'application/vnd.api+json; charset=utf-8');

$this->assertJsonContains([
'data' => [
'id' => $book->getKey(),
'type' => 'Book',
'links' => ['self' => $iri],
'attributes' => [
'name' => $book->name, // @phpstan-ignore-line
],
],
], $response->json());
}
}
7 changes: 7 additions & 0 deletions src/Laravel/config/api-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
'partial_parameter_name' => 'partial',
],

'jsonapi' => [
// When false, the JSON:API `data.id` uses the resource scalar identifier
// and a `data.links.self` IRI is added. When true (default), `data.id`
// is the resource IRI.
'use_iri_as_id' => true,
],

'graphql' => [
'enabled' => false,
'nesting_separator' => '__',
Expand Down
Loading