Skip to content

Commit 9e7fc15

Browse files
derrabusnicolas-grekas
authored andcommitted
[Validator] Deprecate annotations in favor of attributes
1 parent 0a4d3c8 commit 9e7fc15

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
182182
use Symfony\Component\Validator\ObjectInitializerInterface;
183183
use Symfony\Component\Validator\Validation;
184+
use Symfony\Component\Validator\ValidatorBuilder;
184185
use Symfony\Component\Webhook\Controller\WebhookController;
185186
use Symfony\Component\WebLink\HttpHeaderSerializer;
186187
use Symfony\Component\Workflow;
@@ -1609,7 +1610,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
16091610

16101611
if (\array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
16111612
$validatorBuilder->addMethodCall('enableAnnotationMapping', [true]);
1612-
if ($this->isInitializedConfigEnabled('annotations')) {
1613+
if ($this->isInitializedConfigEnabled('annotations') && method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
16131614
$validatorBuilder->addMethodCall('setDoctrineAnnotationReader', [new Reference('annotation_reader')]);
16141615
}
16151616
}

Tests/CacheWarmer/ValidatorCacheWarmerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testWarmUp()
2626
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
2727
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
2828
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
29-
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();
29+
$validatorBuilder->enableAnnotationMapping();
3030

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

5151
$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
5252
@unlink($file);

Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
8282
use Symfony\Component\Validator\Validation;
8383
use Symfony\Component\Validator\Validator\ValidatorInterface;
84+
use Symfony\Component\Validator\ValidatorBuilder;
8485
use Symfony\Component\Webhook\Client\RequestParser;
8586
use Symfony\Component\Webhook\Controller\WebhookController;
8687
use Symfony\Component\Workflow;
@@ -1308,12 +1309,17 @@ public function testValidationLegacyAnnotations()
13081309

13091310
$this->assertCount(8, $calls);
13101311
$this->assertSame('enableAnnotationMapping', $calls[4][0]);
1311-
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
1312-
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
1313-
$this->assertSame('addMethodMapping', $calls[6][0]);
1314-
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
1315-
$this->assertSame('setMappingCache', $calls[7][0]);
1316-
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
1312+
if (method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
1313+
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
1314+
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
1315+
$i = 6;
1316+
} else {
1317+
$i = 5;
1318+
}
1319+
$this->assertSame('addMethodMapping', $calls[$i][0]);
1320+
$this->assertSame(['loadValidatorMetadata'], $calls[$i][1]);
1321+
$this->assertSame('setMappingCache', $calls[++$i][0]);
1322+
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[$i][1]);
13171323
// no cache this time
13181324
}
13191325

Tests/Fixtures/Validation/Category.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class Category
1010

1111
public $id;
1212

13-
/**
14-
* @Assert\Type("string")
15-
*/
13+
#[Assert\Type('string')]
1614
public $name;
1715
}

0 commit comments

Comments
 (0)