Skip to content

[Validator] Autovalidation: skip readonly props #31481

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
May 13, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<service id="validator.property_info_loader" class="Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader">
<argument type="service" id="property_info" />
<argument type="service" id="property_info" />
<argument type="service" id="property_info" />

<tag name="validator.auto_mapper" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Validator\Mapping\Loader;

use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type as PropertyInfoType;
Expand All @@ -29,12 +30,14 @@ final class PropertyInfoLoader implements LoaderInterface
{
private $listExtractor;
private $typeExtractor;
private $accessExtractor;
private $classValidatorRegexp;

public function __construct(PropertyListExtractorInterface $listExtractor, PropertyTypeExtractorInterface $typeExtractor, string $classValidatorRegexp = null)
public function __construct(PropertyListExtractorInterface $listExtractor, PropertyTypeExtractorInterface $typeExtractor, PropertyAccessExtractorInterface $accessExtractor, string $classValidatorRegexp = null)
{
$this->listExtractor = $listExtractor;
$this->typeExtractor = $typeExtractor;
$this->accessExtractor = $accessExtractor;
$this->classValidatorRegexp = $classValidatorRegexp;
}

Expand All @@ -53,6 +56,10 @@ public function loadClassMetadata(ClassMetadata $metadata)
}

foreach ($properties as $property) {
if (false === $this->accessExtractor->isWritable($className, $property)) {
continue;
}

$types = $this->typeExtractor->getTypes($className, $property);
if (null === $types) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ class PropertyInfoLoaderEntity
* })
*/
public $alreadyPartiallyMappedCollection;

public $readOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testLoadClassMetadata()
'alreadyMappedNotNull',
'alreadyMappedNotBlank',
'alreadyPartiallyMappedCollection',
'readOnly',
])
;
$propertyInfoStub
Expand All @@ -58,11 +59,27 @@ public function testLoadClassMetadata()
[new Type(Type::BUILTIN_TYPE_FLOAT, true)], // The existing constraint is float
[new Type(Type::BUILTIN_TYPE_STRING, true)],
[new Type(Type::BUILTIN_TYPE_STRING, true)],
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, null, new Type(Type::BUILTIN_TYPE_FLOAT))]
[new Type(Type::BUILTIN_TYPE_ARRAY, true, null, true, null, new Type(Type::BUILTIN_TYPE_FLOAT))],
[new Type(Type::BUILTIN_TYPE_STRING)]
))
;
$propertyInfoStub
->method('isWritable')
->will($this->onConsecutiveCalls(
true,
true,
true,
true,
true,
true,
true,
true,
true,
false
))
;

$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub);
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub);

$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
Expand Down Expand Up @@ -137,6 +154,9 @@ public function testLoadClassMetadata()
$this->assertSame('string', $alreadyPartiallyMappedCollectionConstraints[0]->constraints[0]->type);
$this->assertInstanceOf(Iban::class, $alreadyPartiallyMappedCollectionConstraints[0]->constraints[1]);
$this->assertInstanceOf(NotNull::class, $alreadyPartiallyMappedCollectionConstraints[0]->constraints[2]);

$readOnlyMetadata = $classMetadata->getPropertyMetadata('readOnly');
$this->assertEmpty($readOnlyMetadata);
}

/**
Expand All @@ -154,7 +174,7 @@ public function testClassValidator(bool $expected, string $classValidatorRegexp
->willReturn([new Type(Type::BUILTIN_TYPE_STRING)])
;

$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $classValidatorRegexp);
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, $classValidatorRegexp);

$classMetadata = new ClassMetadata(PropertyInfoLoaderEntity::class);
$this->assertSame($expected, $propertyInfoLoader->loadClassMetadata($classMetadata));
Expand Down