-
-
Notifications
You must be signed in to change notification settings - Fork 949
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
base: 4.2
Are you sure you want to change the base?
fix(jsonapi): handle missing attributes in ErrorNormalizer #7569
Conversation
7643871 to
87e290e
Compare
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.
87e290e to
918e2fd
Compare
| @@ -0,0 +1,232 @@ | |||
| <?php | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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'])) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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']); | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of them
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.
Description
Fixes an undefined array key warning in
ErrorNormalizerwhen normalizing exceptions that don't have anattributeskey in their normalized structure.Problem
When
ItemNotFoundExceptionor similar exceptions are thrown for invalid resource identifiers, theErrorNormalizer::normalize()method assumes that$jsonApiObject['data']['attributes']exists, causing a warning.Solution
Added defensive checks to handle cases where the
attributeskey is not present, providing a fallback error structure that conforms to the JSON:API specification.