Skip to content

[Validator] Error in ClassMetadata when using cascade constraint on class with union types. #53735

@SherinBloemendaal

Description

@SherinBloemendaal

Symfony version(s) affected

6.4.2

Description

When using the cascade constraint on a class with a union typed property, the ClassMetadata will give an error:
Attempted to call an undefined method named 'getName' of class 'ReflectionUnionType'.

This is caused by ClassMetadata#L201 since $property->getType() returns an ReflectionUnionType.

How to reproduce

<?php

namespace Acme;

use Symfony\Component\Validator\Constraints as Assert;

#[Assert\Cascade]
class AnotherClass 
{
     private null|FirstClass|SecondClass $value = null;
}
<?php

namespace Acme;

use Symfony\Component\Validator\Constraints as Assert;

class FirstClass 
{
     #[Assert\NotBlank]
     private ?string $value = null;
}
<?php

namespace Acme;

use Symfony\Component\Validator\Constraints as Assert;

class SecondClass 
{
     #[Assert\NotBlank]
     private ?string $value = null;
}

Possible Solution

I think we can check if the property type is instance of ReflectionUnionType and if one of the types is an array or the defined class_exists the constraint could be added.

I am busy until end of next week, i can work on a PR.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy