Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Closed
wopoczynski wants to merge2 commits intosymfony:6.3fromwopoczynski:patch-1

Conversation

@wopoczynski
Copy link

@wopoczynskiwopoczynski commentedJul 19, 2023
edited
Loading

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.:

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 reproduce

To reproduce you will need validator, annotations and property access

composer require symfony/validator

example.php:

<?php// composer require symfony/validatoruseDoctrine\Common\Annotations\AnnotationReader;usePHPUnit\Framework\TestCase;useSymfony\Component\Validator\ValidatorBuilder;// parent abstract classabstractclass ParentClass{publicfunction__construct(        #[Assert\NotNull]protected ?string$value        ) {    }}// example child classclass Childextends ParentClass{publicfunction__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 caseclass ExampleTestextends TestCase{/**     * @dataProvider provideValues     */publicfunctiontestValidation(string$value,int$violationCount):void    {$validatorBuilder =newValidatorBuilder();$validatorBuilder->enableAnnotationMapping()            ->setDoctrineAnnotationReader(newAnnotationReader());$validator =$validatorBuilder->getValidator();$child =newChild($value);$this->assertEquals($violationCount,$validator->validate($child)->count());    }/**     * @return array<int, string|int>[]     */publicfunctionprovideValues():array    {return [            ['07127669600',0],            ['35424643507',0],            ['11144477735',0],            ['11144477734',1],            ['11144477725',1],            ['11111111111',1],            ['55555555555',1],            ['aaaaaaaaaaa',1],            ['',1],            ['071276696',2],        ];    }}

Possible Solution

For the path in\Symfony\Component\Validator\Mapping\ClassMetadata::mergeConstraints:349 handle not only private properties but also protected in example:

if ($memberinstanceof MemberMetadata && (!$member->isProtected($this->name) && !$member->isPrivate($this->name))) {
QA
Branch?6.3 for bug fixes
Bug fix?yes
New feature?no
Deprecations?no

szymat and AwesomeGogo reacted with thumbs up emoji
@carsonbot
Copy link

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:

  • Always add tests
  • Keep backward compatibility (seehttps://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (seehttps://symfony.com/releases)
  • Features and deprecations must be submitted against the 6.4 branch.

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!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot
Copy link

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".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

szymat reacted with thumbs down emoji

### 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))) {```
@lyrixxlyrixx removed their request for reviewJuly 19, 2023 13:48
@lyrixx
Copy link
Member

Can you add some tests? Thanks

@szymat
Copy link

I am having same issue after upgrading to 6.3, this needs to be fixed 🙏

AwesomeGogo, dawidf, and barell reacted with thumbs up emoji

@AwesomeGogo
Copy link

AwesomeGogo commentedJul 19, 2023
edited
Loading

@szymat definitely!! +1 I am also facing that issue

barell reacted with thumbs up emoji

@dawidf
Copy link

+1

@wopoczynskiwopoczynski changed the title fix: override of protected property from parent [validator] fix: override of protected property asserts from parentJul 19, 2023
@wopoczynskiwopoczynski changed the title [validator] fix: override of protected property asserts from parent [validator] Fix override of protected property Assertions on Child Class from Parent Class propertyJul 19, 2023
@wopoczynskiwopoczynski changed the title [validator] Fix override of protected property Assertions on Child Class from Parent Class property [Validator] Fix override of protected property Assertions on Child Class from Parent Class propertyJul 19, 2023
@kontsevoye
Copy link

I made almost the same changes in#51027 and I can say that they are relevant forv6.3.1+v6.2.12+v5.4.25+ as well, and not only for protected properties

wopoczynski reacted with thumbs up emoji

@xabbuh
Copy link
Member

This looks like a duplicate of#50788 to me.

szymat reacted with rocket emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@xabbuhxabbuhAwaiting requested review from xabbuh

@dunglasdunglasAwaiting requested review from dunglas

@ycerutoycerutoAwaiting requested review from yceruto

@kbondkbondAwaiting requested review from kbond

@wouterjwouterjAwaiting requested review from wouterj

@chalasrchalasrAwaiting requested review from chalasr

@OskarStarkOskarStarkAwaiting requested review from OskarStark

@jderussejderusseAwaiting requested review from jderusse

Assignees

No one assigned

Projects

None yet

Milestone

6.3

Development

Successfully merging this pull request may close these issues.

9 participants

@wopoczynski@carsonbot@lyrixx@szymat@AwesomeGogo@dawidf@kontsevoye@xabbuh@nicolas-grekas

[8]ページ先頭

©2009-2025 Movatter.jp