Skip to content

[Serializer] Fix JsonSerializableNormalizer ignores circular reference handler in $context #46297

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

Conversation

BreyndotEchse
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? yes
New feature? no
Deprecations? no
License MIT

How to reproduce this bug:
Normalize an object using the default "serializer" service (will use "serializer.normalizer.object" normalizer / ObjectNormalizer)

$target = new class {
    public function getMe() { return $this; }
};
$targetData = $serializer->normalize($target, null, [
    AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static fn($v) => is_object($v) ? get_class($v) : gettype($v)
]);

// Normalized data as expected:
// $targetData === ["me" => "class@anonymous\x00 ...."];

Normalize an object implementing \JsonSerializable using the default "serializer" service (will use "serializer.normalizer.json_serializable" normalizer / JsonSerializableNormalizer)

$target = new class implements \JsonSerializable {
    public function jsonSerialize() { return ['me' => $this]; }
};
$targetData = $serializer->normalize($target, null, [
    AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static fn($v) => is_object($v) ? get_class($v) : gettype($v)
]);

// Same result expected as above, but an exception will be thrown instead:
// Symfony\Component\Serializer\Exception\CircularReferenceException("A circular reference has been detected when serializing the object of class "JsonSerializable@anonymous" (configured limit: 1).")

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@BreyndotEchse BreyndotEchse force-pushed the bugfix/json-serializer-circular-ref-context branch from e8b0e4e to b0189f4 Compare May 9, 2022 18:30
@nicolas-grekas nicolas-grekas force-pushed the bugfix/json-serializer-circular-ref-context branch from b0189f4 to 793a264 Compare May 10, 2022 08:14
@nicolas-grekas
Copy link
Member

Thank you @BreyndotEchse.

@nicolas-grekas nicolas-grekas merged commit 2f33e6e into symfony:4.4 May 10, 2022
@fabpot fabpot mentioned this pull request May 14, 2022
This was referenced May 27, 2022
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.

3 participants