Skip to content

Commit 00b46fa

Browse files
committed
[Validation][FrameworkBundle] Allow EnableAutoMapping to work without auto-mapping namespaces
1 parent 4e44baf commit 00b46fa

File tree

10 files changed

+21
-30
lines changed

10 files changed

+21
-30
lines changed

UPGRADE-4.4.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ Lock
164164
* Deprecated `Symfony\Component\Lock\StoreInterface` in favor of `Symfony\Component\Lock\BlockingStoreInterface` and
165165
`Symfony\Component\Lock\PersistingStoreInterface`.
166166
* `Factory` is deprecated, use `LockFactory` instead
167-
* Deprecated services `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract`,
168-
use `StoreFactory::createStore` instead.
167+
* Deprecated services `lock.store.flock`, `lock.store.semaphore`, `lock.store.memcached.abstract` and `lock.store.redis.abstract`,
168+
use `StoreFactory::createStore` instead.
169169

170170
Mailer
171171
------
@@ -360,6 +360,7 @@ TwigBundle
360360
Validator
361361
---------
362362

363+
* [BC BREAK] Using null as `$classValidatorRegexp` value in `DoctrineLoader::__construct` or `PropertyInfoLoader::__construct` will not enable auto-mapping for all classes anymore, use `'{.*}'` instead.
363364
* Deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`.
364365
* Deprecated using anything else than a `string` as the code of a `ConstraintViolation`, a `string` type-hint will
365366
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`

src/Symfony/Bridge/Doctrine/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.4.0
55
-----
66

7+
* [BC BREAK] using null as `$classValidatorRegexp` value in `DoctrineLoader::__construct` will not enable auto-mapping for all classes anymore, use `'{.*}'` instead.
78
* added `DoctrineClearEntityManagerWorkerSubscriber`
89
* deprecated `RegistryInterface`, use `Doctrine\Persistence\ManagerRegistry`
910
* added support for invokable event listeners

src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use Symfony\Component\Validator\Mapping\TraversalStrategy;
3131
use Symfony\Component\Validator\Tests\Fixtures\Entity;
3232
use Symfony\Component\Validator\Validation;
33-
use Symfony\Component\Validator\ValidatorBuilder;
3433

3534
/**
3635
* @author Kévin Dunglas <[email protected]>
@@ -152,10 +151,6 @@ public function testLoadClassMetadata()
152151

153152
public function testFieldMappingsConfiguration()
154153
{
155-
if (!method_exists(ValidatorBuilder::class, 'addLoader')) {
156-
$this->markTestSkipped('Auto-mapping requires symfony/validation 4.2+');
157-
}
158-
159154
$validator = Validation::createValidatorBuilder()
160155
->addMethodMapping('loadValidatorMetadata')
161156
->enableAnnotationMapping()
@@ -180,7 +175,7 @@ public function testFieldMappingsConfiguration()
180175
*/
181176
public function testClassValidator(bool $expected, string $classValidatorRegexp = null)
182177
{
183-
$doctrineLoader = new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), $classValidatorRegexp);
178+
$doctrineLoader = new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), $classValidatorRegexp, false);
184179

185180
$classMetadata = new ClassMetadata(DoctrineLoaderEntity::class);
186181
$this->assertSame($expected, $doctrineLoader->loadClassMetadata($classMetadata));
@@ -189,21 +184,18 @@ public function testClassValidator(bool $expected, string $classValidatorRegexp
189184
public function regexpProvider()
190185
{
191186
return [
192-
[true, null],
187+
[false, null],
188+
[true, '{.*}'],
193189
[true, '{^'.preg_quote(DoctrineLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
194190
[false, '{^'.preg_quote(Entity::class).'$}'],
195191
];
196192
}
197193

198194
public function testClassNoAutoMapping()
199195
{
200-
if (!method_exists(ValidatorBuilder::class, 'addLoader')) {
201-
$this->markTestSkipped('Auto-mapping requires symfony/validation 4.2+');
202-
}
203-
204196
$validator = Validation::createValidatorBuilder()
205197
->enableAnnotationMapping()
206-
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager()))
198+
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{.*}'))
207199
->getValidator();
208200

209201
$classMetadata = $validator->getMetadataFor(new DoctrineLoaderNoAutoMappingEntity());

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
861861
->end()
862862
->end()
863863
->arrayNode('auto_mapping')
864-
->info('A collection of namespaces for which auto-mapping will be enabled.')
864+
->info('A collection of namespaces for which auto-mapping will be enabled by default, or null to opt-in with the EnableAutoMapping constraint.')
865865
->example([
866866
'App\\Entity\\' => [],
867867
'App\\WithSpecificLoaders\\' => ['validator.property_info_loader'],

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
13121312
}
13131313

13141314
$container->setParameter('validator.auto_mapping', $config['auto_mapping']);
1315-
if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) {
1315+
if (!$propertyInfoEnabled || !class_exists(PropertyInfoLoader::class)) {
13161316
$container->removeDefinition('validator.property_info_loader');
13171317
}
13181318

src/Symfony/Component/Validator/CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `EnableAutoMapping` and `DisableAutoMapping` constraints to enable or disable auto mapping for class or a property
7+
* [BC BREAK] using null as `$classValidatorRegexp` value in `PropertyInfoLoader::__construct` will not enable auto-mapping for all classes anymore, use `'{.*}'` instead.
8+
* added `EnableAutoMapping` and `DisableAutoMapping` constraints to enable or disable auto mapping for class or a property
89
* using anything else than a `string` as the code of a `ConstraintViolation` is deprecated, a `string` type-hint will
910
be added to the constructor of the `ConstraintViolation` class and to the `ConstraintViolationBuilder::setCode()`
1011
method in 5.0
1112
* deprecated passing an `ExpressionLanguage` instance as the second argument of `ExpressionValidator::__construct()`. Pass it as the first argument instead.
12-
* added the `compared_value_path` parameter in violations when using any
13+
* added the `compared_value_path` parameter in violations when using any
1314
comparison constraint with the `propertyPath` option.
1415
* added support for checking an array of types in `TypeValidator`
1516
* added a new `allowEmptyString` option to the `Length` constraint to allow rejecting empty strings when `min` is set, by setting it to `false`.
1617
* Added new `minPropertyPath` and `maxPropertyPath` options
1718
to `Range` constraint in order to get the value to compare
1819
from an array or object
19-
* added the `min_limit_path` and `max_limit_path` parameters in violations when using
20+
* added the `min_limit_path` and `max_limit_path` parameters in violations when using
2021
`Range` constraint with respectively the `minPropertyPath` and
2122
`maxPropertyPath` options
2223
* added a new `notInRangeMessage` option to the `Range` constraint that will

src/Symfony/Component/Validator/DependencyInjection/AddAutoMappingConfigurationPass.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,8 @@ public function process(ContainerBuilder $container)
5959
$validatorBuilder = $container->getDefinition($this->validatorBuilderService);
6060
foreach ($container->findTaggedServiceIds($this->tag) as $id => $tags) {
6161
$regexp = $this->getRegexp(array_merge($globalNamespaces, $servicesToNamespaces[$id] ?? []));
62-
if (null === $regexp) {
63-
$container->removeDefinition($id);
64-
continue;
65-
}
66-
67-
$container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
6862
$validatorBuilder->addMethodCall('addLoader', [new Reference($id)]);
63+
$container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
6964
}
7065

7166
$container->getParameterBag()->remove('validator.auto_mapping');

src/Symfony/Component/Validator/Mapping/Loader/AutoMappingTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ private function isAutoMappingEnabledForClass(ClassMetadata $metadata, string $c
2929
}
3030

3131
// Fallback on the config
32-
return null === $classValidatorRegexp || preg_match($classValidatorRegexp, $metadata->getClassName());
32+
return null !== $classValidatorRegexp && preg_match($classValidatorRegexp, $metadata->getClassName());
3333
}
3434
}

src/Symfony/Component/Validator/Tests/DependencyInjection/AddAutoMappingConfigurationPassTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ public function testDoNotMapAllClassesWhenConfigIsEmpty()
8181

8282
(new AddAutoMappingConfigurationPass())->process($container);
8383

84-
$this->assertFalse($container->hasDefinition('loader'));
84+
$this->assertNull($container->getDefinition('loader')->getArguments()['$classValidatorRegexp'] ?? null);
8585
}
8686
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/PropertyInfoLoaderTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testLoadClassMetadata()
8686
))
8787
;
8888

89-
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub);
89+
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
9090

9191
$validator = Validation::createValidatorBuilder()
9292
->enableAnnotationMapping()
@@ -197,7 +197,8 @@ public function testClassValidator(bool $expected, string $classValidatorRegexp
197197
public function regexpProvider()
198198
{
199199
return [
200-
[true, null],
200+
[false, null],
201+
[true, '{.*}'],
201202
[true, '{^'.preg_quote(PropertyInfoLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
202203
[false, '{^'.preg_quote(Entity::class).'$}'],
203204
];
@@ -217,7 +218,7 @@ public function testClassNoAutoMapping()
217218
[new Type(Type::BUILTIN_TYPE_BOOL)]
218219
);
219220

220-
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub);
221+
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
221222
$validator = Validation::createValidatorBuilder()
222223
->enableAnnotationMapping()
223224
->addLoader($propertyInfoLoader)

0 commit comments

Comments
 (0)