bug #33828 [DoctrineBridge] Auto-validation must work if no regex are passed (dunglas)

This PR was squashed before being merged into the 4.3 branch (closes #33828).

Discussion
----------

[DoctrineBridge] Auto-validation must work if no regex are passed

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Backport of https://github.com/symfony/symfony/pull/32107/files#r295762928.
This behavior if faulty, if no regex are passed, autvalidation must be triggered, [as done in `PropertyInfoLoader`](https://github.com/symfony/symfony/blob/4.3/src/Symfony/Component/Validator/Mapping/Loader/PropertyInfoLoader.php#L50).

Commits
-------

5ed7d6c759 [DoctrineBridge] Auto-validation must work if no regex are passed
This commit is contained in:
Kévin Dunglas 2019-10-29 11:03:42 +01:00
commit ee4b99f227
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
4 changed files with 7 additions and 3 deletions

View File

@ -173,7 +173,7 @@ class DoctrineLoaderTest extends TestCase
public function regexpProvider()
{
return [
[false, null],
[true, null],
[true, '{^'.preg_quote(DoctrineLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
[false, '{^'.preg_quote(Entity::class).'$}'],
];

View File

@ -43,7 +43,7 @@ final class DoctrineLoader implements LoaderInterface
public function loadClassMetadata(ClassMetadata $metadata): bool
{
$className = $metadata->getClassName();
if (null === $this->classValidatorRegexp || !preg_match($this->classValidatorRegexp, $className)) {
if (null !== $this->classValidatorRegexp && !preg_match($this->classValidatorRegexp, $className)) {
return false;
}

View File

@ -59,6 +59,10 @@ class AddAutoMappingConfigurationPass implements CompilerPassInterface
$validatorBuilder = $container->getDefinition($this->validatorBuilderService);
foreach ($container->findTaggedServiceIds($this->tag) as $id => $tags) {
$regexp = $this->getRegexp(array_merge($globalNamespaces, $servicesToNamespaces[$id] ?? []));
if (null === $regexp) {
$container->removeDefinition($id);
continue;
}
$container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
$validatorBuilder->addMethodCall('addLoader', [new Reference($id)]);

View File

@ -81,6 +81,6 @@ class AddAutoMappingConfigurationPassTest extends TestCase
(new AddAutoMappingConfigurationPass())->process($container);
$this->assertNull($container->getDefinition('loader')->getArgument('$classValidatorRegexp'));
$this->assertFalse($container->hasDefinition('loader'));
}
}