Skip to content

Commit

Permalink
bug #31836 [DoctrineBridge] do not process private properties from pa…
Browse files Browse the repository at this point in the history
…rent class (xabbuh)

This PR was merged into the 4.3 branch.

Discussion
----------

[DoctrineBridge] do not process private properties from parent class

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31715, #31752
| License       | MIT
| Doc PR        |

Commits
-------

adfa1ef do not process private properties from parent class
  • Loading branch information
fabpot committed Jun 4, 2019
2 parents 22e5f32 + adfa1ef commit 40076b9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
Expand Up @@ -21,7 +21,7 @@
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class DoctrineLoaderEntity
class DoctrineLoaderEntity extends DoctrineLoaderParentEntity
{
/**
* @ORM\Id
Expand Down
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}
20 changes: 20 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -75,11 +76,30 @@ public function testLoadClassMetadata()
$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');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
Expand Up @@ -81,7 +81,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
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) {
Expand Down

0 comments on commit 40076b9

Please sign in to comment.