From adfa1ef3ceeec1ebaa3fc001a5f00cb59a14426b Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 4 Jun 2019 10:43:25 +0200 Subject: [PATCH] do not process private properties from parent class --- .../Tests/Fixtures/DoctrineLoaderEntity.php | 2 +- .../Fixtures/DoctrineLoaderParentEntity.php | 40 +++++++++++++++++++ .../Tests/Validator/DoctrineLoaderTest.php | 20 ++++++++++ .../Doctrine/Validator/DoctrineLoader.php | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderParentEntity.php diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php index 1f403cb1f8..b6b6a681bd 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderEntity.php @@ -21,7 +21,7 @@ use Symfony\Component\Validator\Constraints as Assert; * * @author Kévin Dunglas */ -class DoctrineLoaderEntity +class DoctrineLoaderEntity extends DoctrineLoaderParentEntity { /** * @ORM\Id diff --git a/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderParentEntity.php b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderParentEntity.php new file mode 100644 index 0000000000..7ec0263559 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/Fixtures/DoctrineLoaderParentEntity.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\Fixtures; + +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\MappedSuperclass + */ +class DoctrineLoaderParentEntity +{ + /** + * @ORM\Column(length=35) + */ + public $publicParentMaxLength; + + /** + * @ORM\Column(length=30) + */ + private $privateParentMaxLength; + + public function getPrivateParentMaxLength() + { + return $this->privateParentMaxLength; + } + + public function setPrivateParentMaxLength($privateParentMaxLength): void + { + $this->privateParentMaxLength = $privateParentMaxLength; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php b/src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php index 3e94da9065..e673d79210 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php @@ -16,6 +16,7 @@ use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; use Symfony\Bridge\Doctrine\Tests\Fixtures\BaseUser; use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEmbed; use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity; +use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderParentEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\DoctrineLoader; use Symfony\Component\Validator\Constraints\Length; @@ -75,11 +76,30 @@ class DoctrineLoaderTest extends TestCase $this->assertSame(10, $alreadyMappedMaxLengthConstraints[0]->max); $this->assertSame(1, $alreadyMappedMaxLengthConstraints[0]->min); + $publicParentMaxLengthMetadata = $classMetadata->getPropertyMetadata('publicParentMaxLength'); + $this->assertCount(1, $publicParentMaxLengthMetadata); + $publicParentMaxLengthConstraints = $publicParentMaxLengthMetadata[0]->getConstraints(); + $this->assertCount(1, $publicParentMaxLengthConstraints); + $this->assertInstanceOf(Length::class, $publicParentMaxLengthConstraints[0]); + $this->assertSame(35, $publicParentMaxLengthConstraints[0]->max); + $embeddedMetadata = $classMetadata->getPropertyMetadata('embedded'); $this->assertCount(1, $embeddedMetadata); $this->assertSame(CascadingStrategy::CASCADE, $embeddedMetadata[0]->getCascadingStrategy()); $this->assertSame(TraversalStrategy::IMPLICIT, $embeddedMetadata[0]->getTraversalStrategy()); + $parentClassMetadata = $validator->getMetadataFor(new DoctrineLoaderParentEntity()); + + $publicParentMaxLengthMetadata = $parentClassMetadata->getPropertyMetadata('publicParentMaxLength'); + $this->assertCount(0, $publicParentMaxLengthMetadata); + + $privateParentMaxLengthMetadata = $parentClassMetadata->getPropertyMetadata('privateParentMaxLength'); + $this->assertCount(1, $privateParentMaxLengthMetadata); + $privateParentMaxLengthConstraints = $privateParentMaxLengthMetadata[0]->getConstraints(); + $this->assertCount(1, $privateParentMaxLengthConstraints); + $this->assertInstanceOf(Length::class, $privateParentMaxLengthConstraints[0]); + $this->assertSame(30, $privateParentMaxLengthConstraints[0]->max); + $embeddedClassMetadata = $validator->getMetadataFor(new DoctrineLoaderEmbed()); $embeddedMaxLengthMetadata = $embeddedClassMetadata->getPropertyMetadata('embeddedMaxLength'); diff --git a/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php b/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php index 111733c1d5..b3993518dd 100644 --- a/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php +++ b/src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php @@ -81,7 +81,7 @@ final class DoctrineLoader implements LoaderInterface if (null === $constraint) { if (isset($mapping['originalClass'])) { $metadata->addPropertyConstraint($mapping['declaredField'], new Valid()); - } else { + } elseif (property_exists($className, $mapping['fieldName'])) { $metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']])); } } elseif (null === $constraint->max) {