Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Serializer] Fix recursive custom normalizer#19532

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
javiereguiluz merged 1 commit intosymfony:7.0frommtarld:fix/custom-normalizer
Feb 8, 2024

Conversation

mtarld
Copy link
Contributor

As mentioned in the following issue:symfony/symfony#53708, the example showing how to create a custom normalizer leads to an infinite recursion.

I could have been fixed like that:

class TopicNormalizer implements NormalizerInterface, NormalizerAwareInterface{    use NormalizerAwareTrait;+    private const ALREADY_CALLED = self::class.'_already_called';+    public function __construct(        private UrlGeneratorInterface $router,    ) {    }    public function normalize($topic, string $format = null, array $context = []): array    {+       $context[self::ALREADY_CALLED] = true;+        $data = $this->normalizer->normalize($topic, $format, $context);        // Here, add, edit, or delete some data:        $data['href']['self'] = $this->router->generate('topic_show', [            'id' => $topic->getId(),        ], UrlGeneratorInterface::ABSOLUTE_URL);        return $data;    }    public function supportsNormalization($data, string $format = null, array $context = []): bool    {+        if ($context[self::ALREADY_CALLED] ?? false) {+            return false;+        }+        return $data instanceof Topic;    }    public function getSupportedTypes(?string $format): array    {        return [-             Topic::class => true,+             Topic::class => false,        ];    }}

But this will prevent the normalizer to be cacheable (because it depends on the context).

Instead, I dropped the use ofNormalizerAwareInterface andNormalizerAwareTrait and used an explicit constructor injection instead.

WDYT?

@javiereguiluz
Copy link
Member

Yes, the solution proposed in this PR looks more elegant than the one you showed in the comment above. Thanks Mathias!

@javiereguiluzjaviereguiluz merged commit9286310 intosymfony:7.0Feb 8, 2024
@mtarldmtarld deleted the fix/custom-normalizer branchFebruary 8, 2024 17:46
@stof
Copy link
Member

autowiring only the object normalizer is a bad idea to me, because it assumes that no other normalizer than the ObjectNormalizer should be responsible for the delegated call.

Note that depending on the context for the support here is not that bad:getSupportedTypes already allows caching that anything that is not a Topic won't be supported (even if the support for things that are Topic cannot be cached)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
7.0
Development

Successfully merging this pull request may close these issues.

4 participants
@mtarld@javiereguiluz@stof@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp