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

Appearance settings

[Validator] Fix regression with class metadatada on parent classes #50788

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

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
[Validator] Fix regression with class metadatada on parent classes
  • Loading branch information
rmikalkenas authored and nicolas-grekas committed Jul 20, 2023
commit e98f5f2782b3bbe849b78f5b486d35b3e57afe8e
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
if (isset($mapping['originalClass']) && !str_contains($mapping['declaredField'], '.')) {
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
$loaded = true;
} elseif (property_exists($className, $mapping['fieldName'])) {
} elseif (property_exists($className, $mapping['fieldName']) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty($mapping['fieldName'])->isPrivate())) {
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
$loaded = true;
}
Expand Down
6 changes: 3 additions & 3 deletions 6 src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ public function mergeConstraints(self $source)

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

if ($member instanceof PropertyMetadata) {
if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
$this->properties[$property] = $member;
} elseif ($member instanceof GetterMetadata) {
} elseif ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
$this->getters[$property] = $member;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Entity extends EntityParent implements EntityInterfaceB
private $internal;
public $data = 'Overridden data';
public $initialized = false;
/**
* @Assert\Type("integer")
*/
protected $other;

public function __construct($internal = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class Entity extends EntityParent implements EntityInterfaceB
private $internal;
public $data = 'Overridden data';
public $initialized = false;
#[Assert\Type('integer')]
protected $other;

public function __construct($internal = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class Entity extends EntityParent implements EntityInterfaceB
private $internal;
public $data = 'Overridden data';
public $initialized = false;
#[Assert\Type('integer')]
protected $other;

public function __construct($internal = null)
{
Expand Down
48 changes: 21 additions & 27 deletions 48 src/Symfony/Component/Validator/Tests/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public function testMergeConstraintsMergesMemberConstraints()
$parent->addPropertyConstraint('firstName', new ConstraintA());
$parent->addPropertyConstraint('firstName', new ConstraintB(['groups' => 'foo']));

$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->mergeConstraints($parent);

$constraintA1 = new ConstraintA(['groups' => [
'Default',
Expand All @@ -179,35 +179,29 @@ public function testMergeConstraintsMergesMemberConstraints()
'groups' => ['foo'],
]);

$constraints = [
$constraintA1,
$constraintB,
$constraintA2,
];
$members = $this->metadata->getPropertyMetadata('firstName');

$constraintsByGroup = [
'Default' => [
$constraintA1,
$constraintA2,
],
'EntityParent' => [
$constraintA1,
],
'Entity' => [
$constraintA1,
$constraintA2,
$this->assertCount(2, $members);
$this->assertEquals(self::CLASSNAME, $members[0]->getClassName());
$this->assertEquals([$constraintA2], $members[0]->getConstraints());
$this->assertEquals(
[
'Default' => [$constraintA2],
'Entity' => [$constraintA2],
],
'foo' => [
$constraintB,
$members[0]->constraintsByGroup
);
$this->assertEquals(self::PARENTCLASS, $members[1]->getClassName());
$this->assertEquals([$constraintA1, $constraintB], $members[1]->getConstraints());
$this->assertEquals(
[
'Default' => [$constraintA1],
'Entity' => [$constraintA1],
'EntityParent' => [$constraintA1],
'foo' => [$constraintB],
],
];

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

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

public function testMemberMetadatas()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Sequentially;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
Expand Down Expand Up @@ -98,6 +99,7 @@ public function testLoadClassMetadata(string $namespace)
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$expected->addPropertyConstraint('other', new Type('integer'));

// load reflection class so that the comparison passes
$expected->getReflectionClass();
Expand Down Expand Up @@ -139,18 +141,16 @@ public function testLoadClassMetadataAndMerge(string $namespace)
$loader->loadClassMetadata($parent_metadata);

$metadata = new ClassMetadata($namespace.'\Entity');
$loader->loadClassMetadata($metadata);

// Merge parent metaData.
$metadata->mergeConstraints($parent_metadata);

$loader->loadClassMetadata($metadata);

$expected_parent = new ClassMetadata($namespace.'\EntityParent');
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();

$expected = new ClassMetadata($namespace.'\Entity');
$expected->mergeConstraints($expected_parent);

$expected->setGroupSequence(['Foo', 'Entity']);
$expected->addConstraint(new ConstraintA());
Expand Down Expand Up @@ -187,11 +187,18 @@ public function testLoadClassMetadataAndMerge(string $namespace)
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$expected->addPropertyConstraint('other', new Type('integer'));

// load reflection class so that the comparison passes
$expected->getReflectionClass();
$expected->mergeConstraints($expected_parent);

$this->assertEquals($expected, $metadata);

$otherMetadata = $metadata->getPropertyMetadata('other');
$this->assertCount(2, $otherMetadata);
$this->assertInstanceOf(Type::class, $otherMetadata[0]->getConstraints()[0]);
$this->assertInstanceOf(NotNull::class, $otherMetadata[1]->getConstraints()[0]);
}

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.