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

Skip to content

[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented May 22, 2025

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

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 the getAllowedAttributes 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.

@carsonbot

This comment was marked as resolved.

@carsonbot carsonbot added this to the 7.3 milestone May 22, 2025
@ruudk ruudk changed the base branch from 7.3 to 6.4 May 22, 2025 11:30
@alexandre-daubois alexandre-daubois modified the milestones: 7.3, 6.4 May 23, 2025
@ruudk
Copy link
Contributor Author

ruudk commented May 23, 2025

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

@ruudk
Copy link
Contributor Author

ruudk commented May 26, 2025

@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 😊

@ruudk ruudk force-pushed the fix-60214 branch 2 times, most recently from ad5ce53 to 930d792 Compare June 19, 2025 11:38
Copy link
Member

@nicolas-grekas nicolas-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

@mtarld mtarld 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!

Fixes symfony#60214

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 the `getAllowedAttributes` 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.
@ruudk
Copy link
Contributor Author

ruudk commented Jun 19, 2025

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-grekas nicolas-grekas merged commit f37df38 into symfony:6.4 Jun 19, 2025
10 of 11 checks passed
@VitekSkrip
Copy link

VitekSkrip commented Jun 19, 2025

Hi @nicolas-grekas

I probably got similar problem at symfony 7.1.5

Test Case:

<?php

declare(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);
    }

}

<?php

declare(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
Contributor Author

ruudk commented Jun 19, 2025

@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.

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.

[Serializer] Discriminator is removed when #[Ignore] attribute used on unrelated method
6 participants