Skip to content
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

[Serializer] Fixing PHP warning in the ObjectNormalizer with MaxDepth enabled #54395

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

jaydiablo
Copy link
Contributor

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #54378
License MIT

This fixes a specific issue I ran into when testing an application in Symfony 6 that uses the rompetompe/inertia-bundle to serialize PHP data to JSON for use by Inertia.js.

Added a test that is as minimal as necessary for the Warning to be thrown. I tested against Symfony 5.4 as well, but the issue doesn't seem to be present there (could have something to do with 5.4 not having the AttributeLoader).

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Can you elaborate a bit on how your fix works? I don't understand it. 😓

@stof
Copy link
Member

stof commented Mar 26, 2024

What happens if attributesMetadata has an entry only for a different attribute ? null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth() would still trigger a warning in such case.

To me, the right fix would be handle properly the case where there is no $attribute key in the array.

@jaydiablo
Copy link
Contributor Author

Regarding the test failure, I'm not sure why that Redis test is failing now, perhaps something in the dependencies updated?

I removed my fix and ran just that test, fails with or without my fix in place, so don't believe it's a result of the change to this PR.

@jaydiablo
Copy link
Contributor Author

Status: Needs Review

@@ -703,7 +703,7 @@ private function updateData(array $data, string $attribute, mixed $attributeValu
private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool
{
if (!($enableMaxDepth = $context[self::ENABLE_MAX_DEPTH] ?? $this->defaultContext[self::ENABLE_MAX_DEPTH] ?? false)
|| null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
Copy link
Member

Choose a reason for hiding this comment

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

Could be:

Suggested change
|| !isset($attributesMetadata[$attribute]) || null === $maxDepth = $attributesMetadata[$attribute]?->getMaxDepth()
|| null === $maxDepth = ($attributesMetadata[$attribute]?->getMaxDepth() ?? null)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The warning is still thrown in that case as $attributesMetadata[$attribute] is still trying to be accessed.

@fabpot
Copy link
Member

fabpot commented Apr 11, 2024

@jaydiablo This PR must be rebased to get rid of the merge commits.

@jaydiablo
Copy link
Contributor Author

@fabpot rebased

@nicolas-grekas
Copy link
Member

Thank you @jaydiablo.

@nicolas-grekas nicolas-grekas merged commit 6e12c6a into symfony:6.4 Apr 12, 2024
1 check was pending
This was referenced Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants