Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Validator] Fix override of protected property Assertions on Child Class from Parent Class property#51025
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
carsonbot commentedJul 19, 2023
Hey! I see that this is your first PR. That is great! Welcome! Symfony has acontribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
carsonbot commentedJul 19, 2023
Hey! Thanks for your PR. You are targeting branch "6.3" but it seems your PR description refers to branch "6.4 for features / 5.4, 6.2, or 6.3 for bug fixes". Cheers! Carsonbot |
### Symfony version(s) affected6.3 and above### DescriptionDuring validation if we are using abstract base class with any Assert on property in it, and on child class we are overriding the property and Asserts current behaviour is enforcing the parent protected property Asserts. I.e.:```abstract class ParentClass{ public function __construct( #[Assert\NotNull] protected ?string $value ) { }}class Child extends ParentClass{ public function __construct( #[Assert\Expression(expression: 'this.isValid() === true', message: 'value is invalid. ')] #[Assert\Length(min: 11, max: 11, exactMessage: 'must be 11 digits long')] #[Assert\NotBlank(message: 'value is required')] #[Assert\Regex(pattern: '/^[0-9]+$/', message: 'Must contain digits only')] protected ?string $value = null ) {}}```### How to reproduceTo reproduce you will need validator, annotations and property access```composer require symfony/validator```example.php:```php<?php// composer require symfony/validatoruse Doctrine\Common\Annotations\AnnotationReader;use PHPUnit\Framework\TestCase;use Symfony\Component\Validator\ValidatorBuilder;// parent abstract classabstract class ParentClass{ public function __construct( #[Assert\NotNull] protected ?string $value ) { }}// example child classclass Child extends ParentClass{ public function __construct( #[Assert\Expression(expression: 'this.isValid() === true', message: 'value is invalid. ')] #[Assert\Length(min: 11, max: 11, exactMessage: 'must be 11 digits long')] #[Assert\NotBlank(message: 'value is required')] #[Assert\Regex(pattern: '/^[0-9]+$/', message: 'Must contain digits only')] protected ?string $value = null ) {}}// example test case class ExampleTest extends TestCase{ /** *@dataProvider provideValues */ public function testValidation(string $value, int $violationCount): void { $validatorBuilder = new ValidatorBuilder(); $validatorBuilder->enableAnnotationMapping() ->setDoctrineAnnotationReader(new AnnotationReader()); $validator = $validatorBuilder->getValidator(); $child = new Child($value); $this->assertEquals($violationCount, $validator->validate($child)->count()); } /** *@return array<int, string|int>[] */ public function provideValues(): array { return [ ['07127669600', 0], ['35424643507', 0], ['11144477735', 0], ['11144477734', 1], ['11144477725', 1], ['11111111111', 1], ['55555555555', 1], ['aaaaaaaaaaa', 1], [' ', 1], ['071276696', 2], ]; }}```### Possible SolutionFor the path in `\Symfony\Component\Validator\Mapping\ClassMetadata::mergeConstraints:349` handle not only private properties but also protectedin example:```php if ($member instanceof MemberMetadata && (!$member->isProtected($this->name) && !$member->isPrivate($this->name))) {```lyrixx commentedJul 19, 2023
Can you add some tests? Thanks |
szymat commentedJul 19, 2023
I am having same issue after upgrading to 6.3, this needs to be fixed 🙏 |
AwesomeGogo commentedJul 19, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@szymat definitely!! +1 I am also facing that issue |
dawidf commentedJul 19, 2023
+1 |
kontsevoye commentedJul 19, 2023
xabbuh commentedJul 20, 2023
This looks like a duplicate of#50788 to me. |
Uh oh!
There was an error while loading.Please reload this page.
Symfony version(s) affected
6.3 and above
Description
During validation if we are using abstract base class with any Assert on property in it, and on child class we are overriding the property and Asserts current behaviour is enforcing the parent protected property Asserts.
I.e.:
How to reproduce
To reproduce you will need validator, annotations and property access
example.php:
Possible Solution
For the path in
\Symfony\Component\Validator\Mapping\ClassMetadata::mergeConstraints:349handle not only private properties but also protected in example: