8000 [Validator] Error in ClassMetadata when using cascade constraint on class with union types. · Issue #53735 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

Closed
SherinBloemendaal opened this issue Feb 2, 2024 · 1 comment

Comments

@SherinBloemendaal
Copy link
Contributor
SherinBloemendaal commented Feb 2, 2024

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

@xabbuh
Copy link
Member
xabbuh commented Apr 29, 2024

see #54760

nicolas-grekas added a commit that referenced this issue May 1, 2024
…ed validations (xabbuh)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] handle union and intersection types for cascaded validations

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #49629, Fix #53735
| License       | MIT

Commits
-------

18e06a8 handle union and intersection types for cascaded validations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0