8000 [Serializer] Add `defaultType` to `DiscriminatorMap` by alanpoulain · Pull Request #59828 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add defaultType to DiscriminatorMap #59828

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&rdq 8000 uo;, 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
Mar 1, 2025

Conversation

alanpoulain
Copy link
Contributor
@alanpoulain alanpoulain commented Feb 21, 2025
Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues N/A
License MIT

I thought it would be nice to have a default type value for the discriminator map of the serializer.

For instance with this configuration:

#[DiscriminatorMap(
    typeProperty: 'type',
    mapping: [
        'article' => Article::class,
        'video' => Video::class,
    ],
    defaultType: 'article'
)]
abstract class Element
{
    public string $product;
}

final class Article extends Element
{
    public string $title;
}

final class Video extends Element
{
    public int $duration;
}

It would deserialize to an Article with this data:

{
    "product": "desk",
    "title": "PHP devs love Symfony"
}

And to a Video with this:

{
    "product": "desk",
    "duration": 8765,
    "type": "video"
}

@alanpoulain alanpoulain force-pushed the discriminator-map-default-value branch from 83f1ca5 to 828656f Compare February 26, 2025 08:38
@alanpoulain alanpoulain force-pushed the discriminator-map-default-value branch from 828656f to c03064a Compare February 26, 2025 13:59
@alanpoulain alanpoulain changed the title [Serializer] Add defaultValue to DiscriminatorMap [Serializer] Add defaultType to DiscriminatorMap Feb 26, 2025
@alanpoulain
Copy link
Contributor Author

Thanks for the review @mtarld!

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.

With minor comments 🙂

@alanpoulain alanpoulain force-pushed the discriminator-map-default-value branch from c03064a to 9d089bd Compare February 26, 2025 16:01
@fabpot
Copy link
Member
fabpot commented Mar 1, 2025

Thank you @alanpoulain.

@fabpot fabpot merged commit cb93a4f into symfony:7.3 Mar 1, 2025
9 of 11 checks passed
@fabpot fabpot mentioned this pull request May 2, 2025
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.

5 participants
0