Skip to content

Commit

Permalink
[FrameworkBundle][Validator] Deprecate annotation occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Aug 18, 2023
1 parent 9a0f178 commit 89a4e3c
Show file tree
Hide file tree
Showing 101 changed files with 600 additions and 188 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ FrameworkBundle
* Deprecate not setting the `framework.uid.default_uuid_version` config option; it will default to `7` in 7.0
* Deprecate not setting the `framework.uid.time_based_uuid_version` config option; it will default to `7` in 7.0
* Deprecate not setting the `framework.validation.email_validation_mode` config option; it will default to `html5` in 7.0
* Deprecate `framework.validation.enable_annotations`, use `framework.validation.enable_attributes` instead
* Deprecate `framework.serializer.enable_annotations`, use `framework.serializer.enable_attributes` instead

HttpFoundation
--------------
Expand Down Expand Up @@ -157,3 +159,6 @@ Validator
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
* Deprecate `AnnotationLoader`, use `AttributeLoader` instead
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

class UniqueEntityTest extends TestCase
{
public function testAttributeWithDefaultProperty()
{
$metadata = new ClassMetadata(UniqueEntityDummyOne::class);
$loader = new AnnotationLoader();
$loader = new AttributeLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

/** @var UniqueEntity $constraint */
Expand All @@ -35,7 +35,7 @@ public function testAttributeWithDefaultProperty()
public function testAttributeWithCustomizedService()
{
$metadata = new ClassMetadata(UniqueEntityDummyTwo::class);
$loader = new AnnotationLoader();
$loader = new AttributeLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

/** @var UniqueEntity $constraint */
Expand All @@ -50,7 +50,7 @@ public function testAttributeWithCustomizedService()
public function testAttributeWithGroupsAndPaylod()
{
$metadata = new ClassMetadata(UniqueEntityDummyThree::class);
$loader = new AnnotationLoader();
$loader = new AttributeLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

/** @var UniqueEntity $constraint */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DoctrineLoaderTest extends TestCase
public function testLoadClassMetadata()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->enableAttributeMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testExtractEnum()
{
$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->enableAnnotationMapping(true)
->enableAttributeMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
Expand All @@ -159,7 +159,7 @@ public function testExtractEnum()
public function testFieldMappingsConfiguration()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->enableAttributeMapping()
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
->addLoader(
new DoctrineLoader(
Expand Down Expand Up @@ -200,7 +200,7 @@ public static function regexpProvider(): array
public function testClassNoAutoMapping()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->enableAttributeMapping()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{.*}'))
->getValidator();

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"symfony/stopwatch": "^5.4|^6.0|^7.0",
"symfony/translation": "^5.4|^6.0|^7.0",
"symfony/uid": "^5.4|^6.0|^7.0",
"symfony/validator": "^5.4.25|~6.2.12|^6.3.1|^7.0",
"symfony/validator": "^6.4|^7.0",
"symfony/var-dumper": "^5.4|^6.0|^7.0",
"doctrine/collections": "^1.0|^2.0",
"doctrine/data-fixtures": "^1.1",
Expand All @@ -63,7 +63,7 @@
"symfony/property-info": "<5.4",
"symfony/security-bundle": "<5.4",
"symfony/security-core": "<6.4",
"symfony/validator": "<5.4.25|>=6,<6.2.12|>=6.3,<6.3.1"
"symfony/validator": "<6.4"
},
"autoload": {
"psr-4": { "Symfony\\Bridge\\Doctrine\\": "" },
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CHANGELOG
* Deprecate not setting the `framework.uid.default_uuid_version` config option; it will default to `7` in 7.0
* Deprecate not setting the `framework.uid.time_based_uuid_version` config option; it will default to `7` in 7.0
* Deprecate not setting the `framework.validation.email_validation_mode` config option; it will default to `html5` in 7.0
* Deprecate `framework.validation.enable_annotations`, use `framework.validation.enable_attributes` instead
* Deprecate `framework.serializer.enable_annotations`, use `framework.serializer.enable_attributes` instead

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,16 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
trigger_deprecation('symfony/framework-bundle', '6.4', 'Not setting the "framework.validation.email_validation_mode" config option is deprecated. It will default to "html5" in 7.0.');
}

if (isset($v['enable_annotations'])) {
trigger_deprecation('symfony/framework-bundle', '6.4', 'Option "enable_annotations" at "framework.validation" is deprecated. Use the "enable_attributes" option instead.');

if (!isset($v['enable_attributes'])) {
$v['enable_attributes'] = $v['enable_annotations'];
} else {
throw new \InvalidArgumentException('The "enable_annotations" and "enable_attributes" options at path "framework.validation" must not be both set. Only the "enable_attributes" option must be used.');
}
}

return $v;
})
->end()
Expand All @@ -1033,7 +1043,8 @@ private function addValidationSection(ArrayNodeDefinition $rootNode, callable $e
->{$enableIfStandalone('symfony/validator', Validation::class)}()
->children()
->scalarNode('cache')->end()
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
->booleanNode('enable_annotations')->end()
->booleanNode('enable_attributes')->{!class_exists(FullStack::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
->arrayNode('static_method')
->defaultValue(['loadValidatorMetadata'])
->prototype('scalar')->end()
Expand Down Expand Up @@ -1139,10 +1150,25 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode, callable $e
$rootNode
->children()
->arrayNode('serializer')
->validate()
->always(function ($v) {
if (isset($v['enable_annotations'])) {
trigger_deprecation('symfony/framework-bundle', '6.4', 'Option "enable_annotations" at "framework.serializer" is deprecated. Use the "enable_attributes" option instead.');

if (!isset($v['enable_attributes'])) {
$v['enable_attributes'] = $v['enable_annotations'];
} else {
throw new \InvalidArgumentException('The "enable_annotations" and "enable_attributes" options at path "framework.serializer" must not be both set. Only the "enable_attributes" option must be used.');
}
}

return $v;
})->end()
->info('serializer configuration')
->{$enableIfStandalone('symfony/serializer', Serializer::class)}()
->children()
->booleanNode('enable_annotations')->{!class_exists(FullStack::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
->booleanNode('enable_annotations')->end()
->booleanNode('enable_attributes')->{!class_exists(FullStack::class) ? 'defaultTrue' : 'defaultFalse'}()->end()
->scalarNode('name_converter')->end()
->scalarNode('circular_reference_handler')->end()
->scalarNode('max_depth_handler')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1657,8 +1657,8 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
$definition = $container->findDefinition('validator.email');
$definition->replaceArgument(0, $config['email_validation_mode']);

if (\array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
$validatorBuilder->addMethodCall('enableAnnotationMapping', [true]);
if (\array_key_exists('enable_attributes', $config) && $config['enable_attributes']) {
$validatorBuilder->addMethodCall('enableAttributeMapping', [true]);
if ($this->isInitializedConfigEnabled('annotations') && method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
$validatorBuilder->addMethodCall('setDoctrineAnnotationReader', [new Reference('annotation_reader')]);
}
Expand Down Expand Up @@ -1930,7 +1930,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
}

$serializerLoaders = [];
if (isset($config['enable_annotations']) && $config['enable_annotations']) {
if (isset($config['enable_attributes']) && $config['enable_attributes']) {
if ($container->getParameter('kernel.debug')) {
$container->removeDefinition('serializer.mapping.cache_class_metadata_factory');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="cache" type="xsd:string" />
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
<xsd:attribute name="enable-attributes" type="xsd:boolean" />
<xsd:attribute name="static-method" type="xsd:boolean" />
<xsd:attribute name="translation-domain" type="xsd:string" />
<xsd:attribute name="strict-email" type="xsd:boolean" />
Expand Down Expand Up @@ -322,6 +323,7 @@
</xsd:choice>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="enable-annotations" type="xsd:boolean" />
<xsd:attribute name="enable-attributes" type="xsd:boolean" />
<xsd:attribute name="name-converter" type="xsd:string" />
<xsd:attribute name="circular-reference-handler" type="xsd:string" />
<xsd:attribute name="max-depth-handler" type="xsd:string" />
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->enableAttributeMapping();

$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->enableAttributeMapping();

$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 @@ -591,7 +591,7 @@ protected static function getBundleDefaultConfig()
],
'validation' => [
'enabled' => !class_exists(FullStack::class),
'enable_annotations' => !class_exists(FullStack::class),
'enable_attributes' => !class_exists(FullStack::class),
'static_method' => ['loadValidatorMetadata'],
'translation_domain' => 'validators',
'mapping' => [
Expand All @@ -612,7 +612,7 @@ protected static function getBundleDefaultConfig()
'serializer' => [
'default_context' => ['foo' => 'bar', JsonDecode::DETAILED_ERROR_MESSAGES => true],
'enabled' => true,
'enable_annotations' => !class_exists(FullStack::class),
'enable_attributes' => !class_exists(FullStack::class),
'mapping' => ['paths' => []],
],
'property_access' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
'annotations' => false,
'serializer' => [
'enabled' => true,
'enable_annotations' => true,
'enable_attributes' => true,
'name_converter' => 'serializer.name_converter.camel_case_to_snake_case',
'circular_reference_handler' => 'my.circular.reference.handler',
'max_depth_handler' => 'my.max.depth.handler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'php_errors' => ['log' => true],
'annotations' => false,
'serializer' => [
'enable_annotations' => true,
'enable_attributes' => true,
'mapping' => [
'paths' => [
'%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'serializer' => [
'enable_annotations' => false,
'enable_attributes' => false,
'mapping' => [
'paths' => [
'%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'secret' => 's3cr3t',
'validation' => [
'enabled' => true,
'enable_annotations' => true,
'enable_attributes' => true,
'email_validation_mode' => 'html5',
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'secret' => 's3cr3t',
'validation' => [
'enabled' => true,
'enable_annotations' => true,
'enable_attributes' => true,
'email_validation_mode' => 'html5',
],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<framework:validation enabled="true" email-validation-mode="html5" />
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:serializer enabled="true" enable-annotations="true" name-converter="serializer.name_converter.camel_case_to_snake_case" circular-reference-handler="my.circular.reference.handler" max-depth-handler="my.max.depth.handler">
<framework:serializer enabled="true" enable-attributes="true" name-converter="serializer.name_converter.camel_case_to_snake_case" circular-reference-handler="my.circular.reference.handler" max-depth-handler="my.max.depth.handler">
<framework:default-context>
<framework:enable_max_depth>true</framework:enable_max_depth>
</framework:default-context>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:serializer enable-annotations="true">
<framework:serializer enable-attributes="true">
<framework:mapping>
<framework:path>%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files</framework:path>
<framework:path>%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml</framework:path>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:serializer enable-annotations="false">
<framework:serializer enable-attributes="false">
<framework:mapping>
<framework:path>%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files</framework:path>
<framework:path>%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/serialization.yml</framework:path>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<framework:config secret="s3cr3t" http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:validation enabled="true" enable-annotations="true" email-validation-mode="html5" />
<framework:validation enabled="true" enable-attributes="true" email-validation-mode="html5" />
</framework:config>

<services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<framework:config secret="s3cr3t" http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="true" />
<framework:php-errors log="true" />
<framework:validation enabled="true" enable-annotations="true" email-validation-mode="html5" />
<framework:validation enabled="true" enable-attributes="true" email-validation-mode="html5" />
</framework:config>

<services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ framework:
annotations: false
serializer:
enabled: true
enable_annotations: true
enable_attributes: true
name_converter: serializer.name_converter.camel_case_to_snake_case
circular_reference_handler: my.circular.reference.handler
max_depth_handler: my.max.depth.handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ framework:
log: true
annotations: false
serializer:
enable_annotations: true
enable_attributes: true
mapping:
paths:
- "%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ framework:
php_errors:
log: true
serializer:
enable_annotations: false
enable_attributes: false
mapping:
paths:
- "%kernel.project_dir%/Fixtures/TestBundle/Resources/config/serializer_mapping/files"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ framework:
secret: s3cr3t
validation:
enabled: true
enable_annotations: true
enable_attributes: true
email_validation_mode: html5

services:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ framework:
secret: s3cr3t
validation:
enabled: true
enable_annotations: true
enable_attributes: true
email_validation_mode: html5

services:
Expand Down

0 comments on commit 89a4e3c

Please sign in to comment.