Skip to content

Conversation

@daFish
Copy link
Contributor

@daFish daFish commented Nov 30, 2025

Fixes undefined array key warning when normalizing exceptions that don't have attributes in their normalized structure.

This typically occurs with ItemNotFoundException when invalid resource identifiers are provided.

Q A
Branch? 4.2
Tickets not applicable
License MIT
Doc PR not applicable

Description

Fixes an undefined array key warning in ErrorNormalizer when normalizing exceptions that don't have an attributes key in their normalized structure.

Problem

When ItemNotFoundException or similar exceptions are thrown for invalid resource identifiers, the ErrorNormalizer::normalize() method assumes that $jsonApiObject['data']['attributes'] exists, causing a warning.

Solution

Added defensive checks to handle cases where the attributes key is not present, providing a fallback error structure that conforms to the JSON:API specification.

@daFish daFish force-pushed the fix/jsonapi-error-normalizer-missing-attributes branch from 7643871 to 87e290e Compare November 30, 2025 16:27
Fixes undefined array key warning when normalizing exceptions
that don't have attributes in their normalized structure.

This typically occurs with ItemNotFoundException when invalid
resource identifiers are provided.
@daFish daFish force-pushed the fix/jsonapi-error-normalizer-missing-attributes branch from 87e290e to 918e2fd Compare November 30, 2025 20:15
@daFish daFish changed the base branch from main to 4.2 November 30, 2025 20:21
@@ -0,0 +1,232 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

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

tests are located in the component itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, right. Will be moved.

{
$jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);

if (!isset($jsonApiObject['data']['attributes'])) {
Copy link
Member

Choose a reason for hiding this comment

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

how does this happen functionally? I'd love a functional reproducer inside tests/Functional/JsonApi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have the issue in my application and I'll come up with a functional test to highlight it.

$this->assertEquals('An error occurred', $result['errors'][0]['title']);
$this->assertEquals('Something went wrong', $result['errors'][0]['detail']);
$this->assertIsString($result['errors'][0]['status']);
}
Copy link
Member

Choose a reason for hiding this comment

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

these unit tests aren't providing useful additions in my opinion I'd rather not have them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of them or only testNormalizeWithValidStructure?

Copy link
Member

Choose a reason for hiding this comment

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

All of them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants