8000 [Validator] Fix constraints merge for protected and public properties by kontsevoye · Pull Request #51027 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Fix constraints merge for protecte 8000 d and public properties #51027

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,17 +356,16 @@ public function mergeConstraints(self $source)
$constraint->addImplicitGroupName($this->getDefaultGroup());
}

$this->addPropertyMetadata($member);

if ($member instanceof MemberMetadata && !$member->isPrivate($this->name)) {
$property = $member->getPropertyName();
$this->members[$property] = [$member];

if ($member instanceof PropertyMetadata) {
$this->properties[$property] = $member;
} elseif ($member instanceof GetterMetadata) {
$this->getters[$property] = $member;
}
} else {
$this->addPropertyMetadata($member);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class EntityParent implements EntityInterfaceA
private $internal;
private $data = 'Data';
private $child;
protected $address;
public $nickname;

/**
* @NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,70 @@ public function testMergeConstraintsKeepsPrivateMembersSeparate()
$this->assertEquals($constraints, $members[1]->getConstraints());
}

public function testMergeConstraintsKeepsProtectedMembersSeparate()
{
$this->metadata->addPropertyConstraint('address', new ConstraintA());

$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('address', new ConstraintB());

$this->metadata->mergeConstraints($parent);

$constraints = [
new ConstraintA(['groups' => [
'Default',
'Entity',
]]),
];
$parentConstraints = [
new ConstraintB(['groups' => [
'Default',
'EntityParent',
'Entity',
]]),
];

$members = $this->metadata->getPropertyMetadata('address');

$this->assertCount(2, $members);
$this->assertEquals(self::CLASSNAME, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
$this->assertEquals(self::PARENTCLASS, $members[1]->getClassName());
$this->assertEquals($parentConstraints, $members[1]->getConstraints());
}

public function testMergeConstraintsKeepsPublicMembersSeparate()
{
$this->metadata->addPropertyConstraint('nickname', new ConstraintA());

$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('nickname', new ConstraintB());

$this->metadata->mergeConstraints($parent);

$constraints = [
new ConstraintA(['groups' => [
'Default',
'Entity',
]]),
];
$parentConstraints = [
new ConstraintB(['groups' => [
'Default',
'EntityParent',
'Entity',
]]),
];

$members = $this->metadata->getPropertyMetadata('nickname');

$this->assertCount(2, $members);
$this->assertEquals(self::CLASSNAME, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
$this->assertEquals(self::PARENTCLASS, $members[1]->getClassName());
$this->assertEquals($parentConstraints, $members[1]->getConstraints());
}

public function testGetReflectionClass()
{
$reflClass = new \ReflectionClass(self::CLASSNAME);
Expand Down
0