Skip to content

Conversation

@Spea
Copy link
Contributor

@Spea Spea commented Nov 28, 2025

With these changes it is now possible to use (union) discriminators. A few examples which are supported might look like this 👇

Examples

Discriminators

#[JMS\Discriminator(field: 'type', map: ['first' => FirstClass::class, 'second' => SecondClass::class])]
abstract class Discriminator
{
}

class FirstClass extends Discriminator
{
    public string $firstProperty;
}

class SecondClass extends Discriminator
{
    public int $secondProperty;
}

Union Discriminators

use JMS\Serializer\Annotation\UnionDiscriminator;

class JmsUnionTyping
{
    #[UnionDiscriminator(field: 'objectType', map: ['comment' => FirstClass::class, 'author' => SecondClass::class])]
    public FirstClass|SecondClass $property;
}

// ...

class PrimitiveUnionTyping
{
    public int|string|false|float|array|null $property;
}

To support this, it is necessary to use the latest changes from the 2.x branch from the metadata-parser (including the changes necessary for union discriminators via liip/metadata-parser#54).
So ideally this change should go into a new 3.x branch where we then release a new major version, as doing a minor release will potentially break peoples implementation since a few things changed in the parse library.

Side note: There is a temporary commit in here which uses the changes from the other PR in the metadata-parser repo. It will be removed/adjusted, once merged and a new version exists.

@Spea Spea force-pushed the discriminator-support branch from cbca271 to f9c0720 Compare November 28, 2025 14:15
@dbu
Copy link
Member

dbu commented Dec 1, 2025

very cool, thanks!

i will do php 8.5 and symfony 8 support on the 2.x branch, then add a 3.x branch where we should merge your changes.

before we tag the 3.0 release, we should look at the deprecation and do the BC break to cleanup.
apart from that i am not aware of things we have to do, are there?

@dbu dbu changed the base branch from 2.x to 3.x December 1, 2025 11:23
@dbu
Copy link
Member

dbu commented Dec 1, 2025

i tagged 2.6.2 and now created the 3.x branch. i changed the target, but github reports conflicts. i did apply the cs fixer in the 2.x version, so that is probably not needed anymore. and i added to the changelog - i added the 2.x and 3.x headings so that future changes in 2.x do not conflict with 3.x anymore.

can you please rebase this on the 3.x branch and solve the conflicts?

@Spea Spea force-pushed the discriminator-support branch from f9c0720 to 0afd0a6 Compare December 1, 2025 13:26
@Spea
Copy link
Contributor Author

Spea commented Dec 1, 2025

before we tag the 3.0 release, we should look at the deprecation and do the BC break to cleanup. apart from that i am not aware of things we have to do, are there?

I removed the deprecation warning and adjusted the code around it. Other than that I think we are good here, couldn't find any other deprecations.

i tagged 2.6.2 and now created the 3.x branch. i changed the target, but github reports conflicts. i did apply the cs fixer in the 2.x version, so that is probably not needed anymore. and i added to the changelog - i added the 2.x and 3.x headings so that future changes in 2.x do not conflict with 3.x anymore.

can you please rebase this on the 3.x branch and solve the conflicts?

I resolved the conflicts now, thanks for taking care of the new branch and the cs fixes!

@Spea Spea marked this pull request as ready for review December 1, 2025 13:30
Spea added 7 commits December 1, 2025 18:06
The `PropertyTypeArray` class was removed in favor the `PropertyTypeIterable` one.
With this change, the `Discriminator` attribute from JMS is now fully supported.
With this change, it is now possible to use primitive union types (like `public int|bool $property`) or the more complex `#[UnionDiscriminator]` attribute from JMS.
Passing a `string` for the `$formats` argument when rendering the assignment of a `DateTime` class was deprecated in `2.x` and thus got for `3.x`.
Those are the more critical BC breaks, so its worth mentioning them here as well. For all further changes, the `CHANGELOG.md` from the `metadata-parser` is also referenced.
@Spea Spea force-pushed the discriminator-support branch from 0afd0a6 to e2521f7 Compare December 1, 2025 17:06
@dbu
Copy link
Member

dbu commented Dec 2, 2025

thanks! i tweaked the changelog entry a bit and merged on the CLI. thanks a lot for the contribution!

i tagged https://github.com/liip/serializer/releases/tag/3.0.0 now

@dbu dbu closed this Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants