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
1 change: 1 addition & 0 deletions src/JsonApi/Serializer/ErrorNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function normalize(mixed $data, ?string $format = null, array $context =
$error['id'] = $jsonApiObject['data']['id'];
if (isset($error['type'])) {
$error['links'] = ['type' => $error['type']];
unset($error['type']);
}

if (!isset($error['code']) && method_exists($data, 'getId')) {
Expand Down
59 changes: 59 additions & 0 deletions src/JsonApi/Tests/Serializer/ErrorNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\JsonApi\Tests\Serializer;

use ApiPlatform\JsonApi\Serializer\ErrorNormalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class ErrorNormalizerTest extends TestCase
{
public function testTypeIsOnlyExposedUnderLinks(): void
{
$itemNormalizer = $this->createStub(NormalizerInterface::class);
$itemNormalizer->method('normalize')->willReturn(['data' => [
'id' => '/errors/500',
'attributes' => ['type' => '/errors/500', 'title' => 'An error occurred', 'status' => 500],
]]);

$errors = (new ErrorNormalizer($itemNormalizer))->normalize(new \Exception('boom'), 'jsonapi')['errors'];

$this->assertCount(1, $errors);
$this->assertArrayNotHasKey('type', $errors[0]);
$this->assertSame(['type' => '/errors/500'], $errors[0]['links']);
$this->assertSame('500', $errors[0]['status']);
}

public function testViolationsAreExposedWithoutTopLevelType(): void
{
$itemNormalizer = $this->createStub(NormalizerInterface::class);
$itemNormalizer->method('normalize')->willReturn(['data' => [
'id' => '/validation_errors/abc',
'attributes' => [
'type' => '/validation_errors/abc',
'title' => 'Validation Failed',
'status' => 422,
'violations' => [['propertyPath' => 'name', 'message' => 'This value should not be blank.']],
],
]]);

$errors = (new ErrorNormalizer($itemNormalizer))->normalize(new \Exception('boom'), 'jsonapi')['errors'];

$this->assertCount(1, $errors);
$this->assertArrayNotHasKey('type', $errors[0]);
$this->assertArrayNotHasKey('violations', $errors[0]);
$this->assertSame(['type' => '/validation_errors/abc/name'], $errors[0]['links']);
$this->assertSame('This value should not be blank.', $errors[0]['detail']);
}
}
1 change: 1 addition & 0 deletions src/Laravel/Tests/JsonApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public function testValidateJsonApi(): void

$this->assertArrayHasKey('id', $json['errors'][0]);
$this->assertArrayHasKey('links', $json['errors'][0]);
$this->assertArrayNotHasKey('type', $json['errors'][0]);
$this->assertArrayHasKey('type', $json['errors'][0]['links']);

$response = $this->postJson(
Expand Down
1 change: 0 additions & 1 deletion src/Laravel/Tests/JsonProblemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public static function formatsProvider(): array
[
'id' => '401',
'detail' => 'Unauthorized',
'type' => 'about:blank',
'title' => 'Error 401',
'status' => 401,
'code' => '401',
Expand Down
6 changes: 4 additions & 2 deletions tests/Functional/JsonApi/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public function testRfc7807ErrorRendersJsonApiFormat(): void
$this->assertSame('An error occurred', $body['errors'][0]['title']);
$this->assertSame('400', $body['errors'][0]['status']);
$this->assertArrayHasKey('detail', $body['errors'][0]);
$this->assertArrayHasKey('type', $body['errors'][0]);
$this->assertArrayNotHasKey('type', $body['errors'][0]);
$this->assertArrayHasKey('type', $body['errors'][0]['links']);
}

public function testNotFoundRouteRendersJsonApiFormat(): void
Expand All @@ -111,6 +112,7 @@ public function testNotFoundRouteRendersJsonApiFormat(): void
$this->assertSame('An error occurred', $body['errors'][0]['title']);
$this->assertSame('404', $body['errors'][0]['status']);
$this->assertArrayHasKey('detail', $body['errors'][0]);
$this->assertArrayHasKey('type', $body['errors'][0]);
$this->assertArrayNotHasKey('type', $body['errors'][0]);
$this->assertArrayHasKey('type', $body['errors'][0]['links']);
}
}
Loading