Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] use the NormalizerAwareInterface to prevent circular references #16826

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

Closed
wants to merge 1 commit into from

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented May 27, 2022

@xabbuh xabbuh changed the title [Serializer] do not depend on concrete NormalizerInterface implementations [Serializer] use the NormalizerAwareInterface to prevent circular references May 27, 2022
@derrabus
Copy link
Member

This implementation would cause an infinite recursion because the serializer would inject itself as normalizer and dispatch the normalize call to our custom normalizer again.

@chalasr
Copy link
Member

chalasr commented May 27, 2022

The infinite recursion can be avoided with this trick: https://symfonycasts.com/screencast/api-platform-security/normalizer-aware#avoiding-recursion-with-a-context-flan

@mtarld
Copy link
Contributor

mtarld commented May 31, 2022

As written in the API Platform documentation, this way to do is actually working if adding a flag in the context.

@HeahDude
Copy link
Contributor

HeahDude commented Jun 9, 2022

I guess we can close here and open a new PR to document symfony/symfony#46625.

@chalasr
Copy link
Member

chalasr commented Jun 9, 2022

@HeahDude Not sure actually :) This example is what prevents us from enabling the serializer panel by default as it builds on an autowired concrete class instead of an interface.

The alternatives I can think of:

  • Use the NormalizerAwareInterface as currently proposed (with the suggested context flag trick to avoid recursion)
  • Change the parameter type declaration from ObjectNormalizer to NormalizerInterface and add the config needed to make it gets the serializer.normalizer.object service injected

If we are not happy with both approaches, that probably means we need to work on adding a better way to give ObjectNormalizer capabilities to a userland normalizer (e.g. a trait or a more scoped interface)

Copy link
Contributor

@maxhelias maxhelias left a comment

Choose a reason for hiding this comment

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

As written in the API Platform documentation, this way to do is actually working if adding a flag in the context.

So we need to add a mention like the API P doc to explain the flag in context.

@chalasr
Copy link
Member

chalasr commented Sep 20, 2022

Thinking twice about this, I'd advocate for the explicit DI approach (second alternative in #16826 (comment))

@javiereguiluz
Copy link
Member

What should we do with this PR? Merge, close or keep working on it? Thanks!

@OskarStark OskarStark changed the title [Serializer] use the NormalizerAwareInterface to prevent circular references [Serializer] use the NormalizerAwareInterface to prevent circular references Oct 26, 2022
@mtarld
Copy link
Contributor

mtarld commented Oct 26, 2022

I think it misses two things:

  1. Add a flag in a context to prevent infinite recursions (here, the normalizer will call himself infinitely)
  2. Maybe add an example to inject a specific implementation of a normalizer thanks to container configuration (here it'll call the chain normalizer)

At least one of these points should be done to prevent infinite recursion.
Either using a flag or injecting a specific implementation will solve that issue, but IMHO, both of these methods might be documented.

@xabbuh
Copy link
Member Author

xabbuh commented Feb 27, 2024

closing as I think this has been fixed in #19532

@xabbuh xabbuh closed this Feb 27, 2024
@xabbuh xabbuh deleted the symfony-46471 branch February 27, 2024 15:22
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.

8 participants