Skip to content

[Validator] Allow load mappings from attributes without doctrine/annotations #39032

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
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] Allow load mappings from attributes without doctrine/anno…
…tations.
  • Loading branch information
derrabus committed Nov 7, 2020
commit 441c80603e77ceccb1e56db80086220fd80b92ca
30 changes: 30 additions & 0 deletions UPGRADE-5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ Validator

* Deprecated the `NumberConstraintTrait` trait.

* Deprecated setting a Doctrine annotation reader via `ValidatorBuilder::enableAnnotationMapping()`

Before:

```php
$builder->enableAnnotationMapping($reader);
```

After:

```php
$builder->enableAnnotationMapping(true)
->setDoctrineAnnotationReader($reader);
```

* Deprecated creating a Doctrine annotation reader via `ValidatorBuilder::enableAnnotationMapping()`

Before:

```php
$builder->enableAnnotationMapping();
```

After:

```php
$builder->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader();
```

Security
--------

Expand Down
30 changes: 30 additions & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ Validator

* Removed the `NumberConstraintTrait` trait.

* `ValidatorBuilder::enableAnnotationMapping()` does not accept a Doctrine annotation reader anymore.

Before:

```php
$builder->enableAnnotationMapping($reader);
```

After:

```php
$builder->enableAnnotationMapping(true)
->setDoctrineAnnotationReader($reader);
```

* `ValidatorBuilder::enableAnnotationMapping()` won't automatically setup a Doctrine annotation reader anymore.

Before:

```php
$builder->enableAnnotationMapping();
```

After:

```php
$builder->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader();
```

Yaml
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ protected function setUp(): void
public function testLoadClassMetadata()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
Expand Down Expand Up @@ -151,7 +152,8 @@ public function testLoadClassMetadata()
public function testFieldMappingsConfiguration()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
->addLoader(
new DoctrineLoader(
Expand Down Expand Up @@ -192,7 +194,8 @@ public function regexpProvider()
public function testClassNoAutoMapping()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{.*}'))
->getValidator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,11 +1303,14 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
$definition->replaceArgument(0, $config['email_validation_mode']);

if (\array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
if (!$this->annotationsConfigEnabled) {
throw new \LogicException('"enable_annotations" on the validator cannot be set as Annotations support is disabled.');
if (!$this->annotationsConfigEnabled && \PHP_VERSION_ID < 80000) {
throw new \LogicException('"enable_annotations" on the validator cannot be set as Doctrine Annotations support is disabled.');
}

$validatorBuilder->addMethodCall('enableAnnotationMapping', [new Reference('annotation_reader')]);
$validatorBuilder->addMethodCall('enableAnnotationMapping', [true]);
if ($this->annotationsConfigEnabled) {
$validatorBuilder->addMethodCall('setDoctrineAnnotationReader', [new Reference('annotation_reader')]);
}
}

if (\array_key_exists('static_method', $config) && $config['static_method']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testWarmUp()
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
$validatorBuilder->enableAnnotationMapping();
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();

$file = sys_get_temp_dir().'/cache-validator.php';
@unlink($file);
Expand All @@ -46,7 +46,7 @@ public function testWarmUpWithAnnotations()
{
$validatorBuilder = new ValidatorBuilder();
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
$validatorBuilder->enableAnnotationMapping();
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();

$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
@unlink($file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ public function testValidation()

$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);

$this->assertCount($annotations ? 7 : 6, $calls);
$this->assertCount($annotations ? 8 : 6, $calls);
$this->assertSame('setConstraintValidatorFactory', $calls[0][0]);
$this->assertEquals([new Reference('validator.validator_factory')], $calls[0][1]);
$this->assertSame('setTranslator', $calls[1][0]);
Expand All @@ -882,6 +882,7 @@ public function testValidation()
$i = 3;
if ($annotations) {
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
$this->assertSame('setDoctrineAnnotationReader', $calls[++$i][0]);
}
$this->assertSame('addMethodMapping', $calls[++$i][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[$i][1]);
Expand Down Expand Up @@ -923,13 +924,14 @@ public function testValidationAnnotations()

$calls = $container->getDefinition('validator.builder')->getMethodCalls();

$this->assertCount(7, $calls);
$this->assertCount(8, $calls);
$this->assertSame('enableAnnotationMapping', $calls[4][0]);
$this->assertEquals([new Reference('annotation_reader')], $calls[4][1]);
$this->assertSame('addMethodMapping', $calls[5][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[5][1]);
$this->assertSame('setMappingCache', $calls[6][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[6][1]);
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
$this->assertSame('addMethodMapping', $calls[6][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
$this->assertSame('setMappingCache', $calls[7][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
// no cache this time
}

Expand All @@ -944,14 +946,15 @@ public function testValidationPaths()

$calls = $container->getDefinition('validator.builder')->getMethodCalls();

$this->assertCount(8, $calls);
$this->assertCount(9, $calls);
$this->assertSame('addXmlMappings', $calls[3][0]);
$this->assertSame('addYamlMappings', $calls[4][0]);
$this->assertSame('enableAnnotationMapping', $calls[5][0]);
$this->assertSame('addMethodMapping', $calls[6][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
$this->assertSame('setMappingCache', $calls[7][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
$this->assertSame('setDoctrineAnnotationReader', $calls[6][0]);
$this->assertSame('addMethodMapping', $calls[7][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[7][1]);
$this->assertSame('setMappingCache', $calls[8][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[8][1]);

$xmlMappings = $calls[3][1][0];
$this->assertCount(3, $xmlMappings);
Expand Down Expand Up @@ -1004,11 +1007,12 @@ public function testValidationNoStaticMethod()

$annotations = !class_exists(FullStack::class) && class_exists(Annotation::class);

$this->assertCount($annotations ? 6 : 5, $calls);
$this->assertCount($annotations ? 7 : 5, $calls);
$this->assertSame('addXmlMappings', $calls[3][0]);
$i = 3;
if ($annotations) {
$this->assertSame('enableAnnotationMapping', $calls[++$i][0]);
$this->assertSame('setDoctrineAnnotationReader', $calls[++$i][0]);
}
$this->assertSame('setMappingCache', $calls[++$i][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[$i][1]);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ CHANGELOG
* added support for UUIDv6 in `Uuid` constraint
* enabled the validator to load constraints from PHP attributes
* deprecated the `NumberConstraintTrait` trait
* deprecated setting or creating a Doctrine annotation reader via `ValidatorBuilder::enableAnnotationMapping()`, pass `true` as first parameter and additionally call `setDoctrineAnnotationReader()` or `addDefaultDoctrineAnnotationReader()` to set up the annotation reader

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ValidValidatorTest extends TestCase
public function testPropertyPathsArePassedToNestedContexts()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
$validator = $validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader()->getValidator();

$violations = $validator->validate(new Foo(), null, ['nested']);

Expand All @@ -23,7 +23,7 @@ public function testPropertyPathsArePassedToNestedContexts()
public function testNullValues()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
$validator = $validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader()->getValidator();

$foo = new Foo();
$foo->fooBar = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function testLoadClassMetadata()
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');

$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader($propertyInfoLoader)
->getValidator()
;
Expand Down Expand Up @@ -220,7 +221,8 @@ public function testClassNoAutoMapping()

$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader($propertyInfoLoader)
->getValidator()
;
Expand Down
71 changes: 71 additions & 0 deletions src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@

namespace Symfony\Component\Validator\Tests;

use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\ValidatorBuilder;

class ValidatorBuilderTest extends TestCase
{
use ExpectDeprecationTrait;

/**
* @var ValidatorBuilder
*/
Expand Down Expand Up @@ -74,9 +80,74 @@ public function testAddMethodMappings()
$this->assertSame($this->builder, $this->builder->addMethodMappings([]));
}

/**
* @group legacy
*/
public function testEnableAnnotationMapping()
{
$this->expectDeprecation('Since symfony/validator 5.2: Not passing true as first argument to "Symfony\Component\Validator\ValidatorBuilder::enableAnnotationMapping" is deprecated. Pass true and call "addDefaultDoctrineAnnotationReader()" if you want to enable annotation mapping with Doctrine Annotations.');
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());

$loaders = $this->builder->getLoaders();
$this->assertCount(1, $loaders);
$this->assertInstanceOf(AnnotationLoader::class, $loaders[0]);

$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
$r->setAccessible(true);

$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
}

public function testEnableAnnotationMappingWithDefaultDoctrineAnnotationReader()
{
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping(true));
$this->assertSame($this->builder, $this->builder->addDefaultDoctrineAnnotationReader());

$loaders = $this->builder->getLoaders();
$this->assertCount(1, $loaders);
$this->assertInstanceOf(AnnotationLoader::class, $loaders[0]);

$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
$r->setAccessible(true);

$this->assertInstanceOf(CachedReader::class, $r->getValue($loaders[0]));
}

/**
* @group legacy
*/
public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReaderLegacy()
{
$reader = $this->createMock(Reader::class);

$this->expectDeprecation('Since symfony/validator 5.2: Passing an instance of "'.\get_class($reader).'" as first argument to "Symfony\Component\Validator\ValidatorBuilder::enableAnnotationMapping" is deprecated. Pass true instead and call setDoctrineAnnotationReader() if you want to enable annotation mapping with Doctrine Annotations.');
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping($reader));

$loaders = $this->builder->getLoaders();
$this->assertCount(1, $loaders);
$this->assertInstanceOf(AnnotationLoader::class, $loaders[0]);

$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
$r->setAccessible(true);

$this->assertSame($reader, $r->getValue($loaders[0]));
}

public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReader()
{
$reader = $this->createMock(Reader::class);

$this->assertSame($this->builder, $this->builder->enableAnnotationMapping(true));
$this->assertSame($this->builder, $this->builder->setDoctrineAnnotationReader($reader));

$loaders = $this->builder->getLoaders();
$this->assertCount(1, $loaders);
$this->assertInstanceOf(AnnotationLoader::class, $loaders[0]);

$r = new \ReflectionProperty(AnnotationLoader::class, 'reader');
$r->setAccessible(true);

$this->assertSame($reader, $r->getValue($loaders[0]));
}

public function testDisableAnnotationMapping()
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy