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] Add support for discriminator map in property normalizer#60511

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
nicolas-grekas merged 1 commit intosymfony:6.4fromruudk:fix-60214
Jun 19, 2025

Conversation

ruudk
Copy link
Contributor

QA
Branch?6.4
Bug fix?yes
New feature?no
Deprecations?no
IssuesFix#60214
LicenseMIT

Currently it's not possible to serialize an object using the PropertyNormalizer when a DiscriminatorMap attribute is used.

It produces the following error:

Symfony\Component\Serializer\Exception\NotNormalizableValueException: Type property "type" not found
for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".

The ObjectNormalizer overrides thegetAllowedAttributes from AbstractNormalizer and adds support for discriminators. But the PropertyNormalizer does not do this. Therefore it doesn't work.

For now, we copy the logic from ObjectNormalizer to PropertyNormalizer and the problem goes away.

rvanlaak reacted with rocket emoji
@carsonbot

This comment was marked as resolved.

@ruudk
Copy link
ContributorAuthor

@derrabus You're probably very busy, but I'm really curious to hear your feedback on this 😊

@ruudk
Copy link
ContributorAuthor

@mtarld@nicolas-grekas Friendly ping to pick your brain on this topic, since you worked on the discriminator mapping in#52681. I'm curious to hear if my approach makes sense, or that I should investigate time in finding an alternative solution. Any advice is much appreciated, thanks 😊

@ruudkruudkforce-pushed thefix-60214 branch 2 times, most recently fromad5ce53 to930d792CompareJune 19, 2025 11:38
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

Works for me. BC is not affected since no API changes. The behavior change is the bug fix so no regression either to me.

Copy link
Contributor

@mtarldmtarld left a comment

Choose a reason for hiding this comment

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

One last thing, otherwise, LGTM!

Fixessymfony#60214Currently it's not possible to serialize an object using the PropertyNormalizer when aDiscriminatorMap attribute is used.It produces the following error:> Symfony\Component\Serializer\Exception\NotNormalizableValueException: Type property "type" not found> for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface".The ObjectNormalizer overrides the `getAllowedAttributes` from AbstractNormalizer and adds support fordiscriminators. But the PropertyNormalizer does not do this. Therefore it doesn't work.For now, we copy the logic from ObjectNormalizer to PropertyNormalizer and the problem goes away.
@ruudk
Copy link
ContributorAuthor

Thanks for the help@mtarld and@nicolas-grekas 🎉

The failing test is unrelated to these changes.

@nicolas-grekas
Copy link
Member

Thank you@ruudk.

@nicolas-grekasnicolas-grekas merged commitf37df38 intosymfony:6.4Jun 19, 2025
10 of 11 checks passed
@VitekSkrip
Copy link

VitekSkrip commentedJun 19, 2025
edited
Loading

Hi@nicolas-grekas

I probably got similar problem at symfony 7.1.5

Test Case:

<?phpdeclare(strict_types=1);namespace App\Test;use Symfony\Component\Serializer\Attribute\DiscriminatorMap;use Symfony\Component\Serializer\Attribute\Ignore;#[DiscriminatorMap(    typeProperty: 'type',    mapping: [        'base_payload' => BasePayload::class,        'new_payload' => ExamplePayload::class,    ],)]readonly class BasePayload{    public function __construct(        protected string $a,    ) {    }    public function getA(): string    {        return $this->a;    }    #[Ignore]    public function isAString(): bool    {        return is_string($this->a);    }}<?phpdeclare(strict_types=1);namespace App\Test;final readonly class ExamplePayload extends BasePayload{    public function __construct(        string $a,        protected string $b,    ) {        parent::__construct($a);    }    public function getB(): string    {        return $this->b;    }}

Runs this:
$payload = new BasePayload('string');

$serialized = $this->serializer->serialize($payload, 'json');

    I got

image

But then i drop Ignore attribute - it passes!!

{"type":"base_payload","a":"string","aString":true}

@nicolas-grekas
Copy link
Member

@VitekSkrip please give this PR a try and create a new issue if it doesn't fix it for you then.

@VitekSkrip
Copy link

@VitekSkrip please give this PR a try and create a new issue if it doesn't fix it for you then.

what version of symfony or symfony/serializer should i use then?

@ruudk
Copy link
ContributorAuthor

ruudk commentedJun 19, 2025
edited
Loading

@VitekSkrip This was just merged to 6.4.x.dev. It will take some time before it is merged to 7.2 and 7.3. Since you are on 7.1 you won't get it, unless you upgrade.

rvanlaak reacted with heart emoji

This was referencedJun 28, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@mtarldmtarldmtarld approved these changes

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
6.4
Development

Successfully merging this pull request may close these issues.

[Serializer] Discriminator is removed when#[Ignore] attribute used on unrelated method
6 participants
@ruudk@carsonbot@nicolas-grekas@VitekSkrip@mtarld@alexandre-daubois

[8]ページ先頭

©2009-2025 Movatter.jp