Skip to content

[PropertyInfo][TypeInfo] Union Type sorts the types it contains #60100

Open
@vuryss

Description

@vuryss

Symfony version(s) affected

7.2.x

Description

I know property info is trying to integrate more and more with the new now stable type-info component, slowly trying to move away from the internal type - now aliased as "legacy type" inside the codebase. However one thing has definitely changed, which for our project is a breaking change - the UnionType internally sorts the types by name:

https://github.com/symfony/type-info/blob/c4824a6b658294c828e609d3d8dbb4e87f6a375d/Type/UnionType.php#L58

I have no idea why this was implemented but we have quite a few cases where the order of the union type arguments is important. One big case is the deserialization process - if one property can be one of multiple types - we try to deserialize in the correct order - and which objects succeeds first - has priority. Some objects have similar structure, so order is important. The old way property info works does not sort the union types.

How to reproduce

For example the following setup:

class Car
{
    public string $make;
}

class Truck
{
    public string $make;
}

class Wrapper
{
    /**
     * @var Truck[]|Car[]
     */
    public array $property;
}

Will return a union type of 2 CollectionTypes, first one will be Car, second one will be Truck. This is if we're using the new PropertyInfo->getType() method, instead of the "legacy one" PropertyInfo->getTypes().

Possible Solution

Do not sort the types inside the union type.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions