-
-
Notifications
You must be signed in to change notification settings - Fork 954
fix(jsonapi): handle missing attributes in ErrorNormalizer #7569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
daFish
wants to merge
1
commit into
api-platform:4.2
Choose a base branch
from
daFish:fix/jsonapi-error-normalizer-missing-attributes
base: 4.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| <?php | ||
daFish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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; | ||
|
|
||
| /** | ||
| * Tests for the JSON API ErrorNormalizer. | ||
| */ | ||
| final class ErrorNormalizerTest extends TestCase | ||
| { | ||
| /** | ||
| * Test normalization when attributes are missing from the normalized structure. | ||
| * This can occur with ItemNotFoundException or similar exceptions. | ||
| * The normalizer should handle this gracefully and return a valid JSON:API error. | ||
| */ | ||
| public function testNormalizeWithMissingAttributes(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'id' => 'error-1', | ||
| 'type' => 'errors', | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $exception = new \Exception('Test error'); | ||
|
|
||
| $result = $errorNormalizer->normalize($exception, 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result); | ||
| $this->assertIsArray($result['errors']); | ||
| $this->assertCount(1, $result['errors']); | ||
| $this->assertEquals('error-1', $result['errors'][0]['id']); | ||
| $this->assertEquals('Test error', $result['errors'][0]['title']); | ||
| $this->assertArrayHasKey('status', $result['errors'][0]); | ||
| } | ||
|
|
||
| public function testNormalizeWithValidStructure(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'type' => 'errors', | ||
| 'id' => 'error-1', | ||
| 'attributes' => [ | ||
| 'title' => 'An error occurred', | ||
| 'detail' => 'Something went wrong', | ||
| 'status' => '500', | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $result = $errorNormalizer->normalize(new \Exception('Test error'), 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result); | ||
| $this->assertCount(1, $result['errors']); | ||
| $this->assertEquals('error-1', $result['errors'][0]['id']); | ||
| $this->assertEquals('An error occurred', $result['errors'][0]['title']); | ||
| $this->assertEquals('Something went wrong', $result['errors'][0]['detail']); | ||
| $this->assertIsString($result['errors'][0]['status']); | ||
| } | ||
daFish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function testNormalizeWithViolations(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'type' => 'errors', | ||
| 'id' => 'validation-error', | ||
| 'attributes' => [ | ||
| 'title' => 'Validation failed', | ||
| 'detail' => 'Invalid input', | ||
| 'status' => 422, | ||
| 'violations' => [ | ||
| [ | ||
| 'message' => 'This field is required', | ||
| 'propertyPath' => 'name', | ||
| ], | ||
| [ | ||
| 'message' => 'Invalid email format', | ||
| 'propertyPath' => 'email', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $result = $errorNormalizer->normalize(new \Exception('Validation error'), 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result); | ||
| $this->assertCount(2, $result['errors']); | ||
| $this->assertEquals('This field is required', $result['errors'][0]['detail']); | ||
| $this->assertEquals('Invalid email format', $result['errors'][1]['detail']); | ||
| $this->assertFalse(isset($result['errors'][0]['violations'])); | ||
| $this->assertIsInt($result['errors'][0]['status']); | ||
| $this->assertEquals(422, $result['errors'][0]['status']); | ||
| } | ||
|
|
||
| /** | ||
| * Test with type and links generation. | ||
| */ | ||
| public function testNormalizeWithTypeGeneratesLinks(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'type' => 'errors', | ||
| 'id' => 'about:blank/errors/validation', | ||
| 'attributes' => [ | ||
| 'type' => 'about:blank/errors/validation', | ||
| 'title' => 'Validation Error', | ||
| 'detail' => 'Input validation failed', | ||
| 'status' => '422', | ||
| 'violations' => [ | ||
| [ | ||
| 'message' => 'Must be a number', | ||
| 'propertyPath' => 'age', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $result = $errorNormalizer->normalize(new \Exception('Validation'), 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result); | ||
| $this->assertCount(1, $result['errors']); | ||
| $this->assertArrayHasKey('links', $result['errors'][0]); | ||
| $this->assertStringContainsString('age', $result['errors'][0]['links']['type']); | ||
| } | ||
|
|
||
| public function testJsonApiComplianceForMissingAttributesCase(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'id' => 'error-123', | ||
| 'type' => 'errors', | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $result = $errorNormalizer->normalize(new \Exception('Not found'), 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result, 'Response must have "errors" key at top level'); | ||
| $this->assertIsArray($result['errors'], '"errors" must be an array'); | ||
| $this->assertNotEmpty($result['errors'], '"errors" array must not be empty'); | ||
|
|
||
| $error = $result['errors'][0]; | ||
| $this->assertIsArray($error, 'Each error must be an object/array'); | ||
|
|
||
| $hasAtLeastOneMember = isset($error['id']) || isset($error['links']) || isset($error['status']) | ||
| || isset($error['code']) || isset($error['title']) || isset($error['detail']) | ||
| || isset($error['source']) || isset($error['meta']); | ||
|
|
||
| $this->assertTrue($hasAtLeastOneMember, 'Error object must contain at least one of: id, links, status, code, title, detail, source, meta'); | ||
|
|
||
| if (isset($error['status'])) { | ||
| $this->assertIsString($error['status'], '"status" must be a string value'); | ||
| } | ||
|
|
||
| if (isset($error['code'])) { | ||
| $this->assertIsString($error['code'], '"code" must be a string value'); | ||
| } | ||
|
|
||
| if (isset($error['links'])) { | ||
| $this->assertIsArray($error['links'], '"links" must be an object'); | ||
| } | ||
| } | ||
|
|
||
| public function testJsonApiComplianceForNormalCase(): void | ||
| { | ||
| $itemNormalizer = $this->createMock(NormalizerInterface::class); | ||
| $itemNormalizer->method('normalize')->willReturn([ | ||
| 'data' => [ | ||
| 'type' => 'errors', | ||
| 'id' => 'error-456', | ||
| 'attributes' => [ | ||
| 'title' => 'Validation Failed', | ||
| 'detail' => 'The request body is invalid', | ||
| 'status' => '422', | ||
| 'code' => 'validation_error', | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $errorNormalizer = new ErrorNormalizer($itemNormalizer); | ||
| $result = $errorNormalizer->normalize(new \Exception('Validation error'), 'jsonapi'); | ||
|
|
||
| $this->assertArrayHasKey('errors', $result); | ||
| $this->assertIsArray($result['errors']); | ||
|
|
||
| $error = $result['errors'][0]; | ||
| $this->assertIsArray($error); | ||
|
|
||
| $hasAtLeastOneMember = isset($error['id']) || isset($error['links']) || isset($error['status']) | ||
| || isset($error['code']) || isset($error['title']) || isset($error['detail']) | ||
| || isset($error['source']) || isset($error['meta']); | ||
|
|
||
| $this->assertTrue($hasAtLeastOneMember, 'Error object must contain at least one required member'); | ||
|
|
||
| $this->assertEquals('error-456', $error['id']); | ||
| $this->assertEquals('Validation Failed', $error['title']); | ||
| $this->assertEquals('The request body is invalid', $error['detail']); | ||
| $this->assertEquals('422', $error['status']); | ||
| $this->assertEquals('validation_error', $error['code']); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.