Skip to content

Commit

Permalink
bug #26823 [Validator] Fix LazyLoadingMetadataFactory with PSR6Cache …
Browse files Browse the repository at this point in the history
…for non classname if tested values isn't existing class (Pascal Montoya, pmontoya)

This PR was merged into the 2.7 branch.

Discussion
----------

[Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non classname if tested values isn't existing class

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26313
| License       | MIT

If @Assert\Valid is applied to a string value, the value is searched in metadata cache and some characters aren't allowed in this cache. This create an unexpected exception.

Class existence is now tested before cache read.

Commits
-------

5198f43 Disable autoloader call on interface_exists check
cd91420 [Validator] Fix LazyLoadingMetadataFactory with PSR6Cache for non classname if tested values isn't an existing class
  • Loading branch information
nicolas-grekas committed Apr 9, 2018
2 parents 16edba5 + 5198f43 commit 0f9c45e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Expand Up @@ -90,17 +90,17 @@ public function getMetadataFor($value)
return $this->loadedClasses[$class];
}

if (!class_exists($class) && !interface_exists($class, false)) {
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
}

if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
// Include constraints from the parent class
$this->mergeConstraints($metadata);

return $this->loadedClasses[$class] = $metadata;
}

if (!class_exists($class) && !interface_exists($class)) {
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
}

$metadata = new ClassMetadata($class);

if (null !== $this->loader) {
Expand Down
Expand Up @@ -149,6 +149,21 @@ public function testReadMetadataFromCache()
$this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS));
}

/**
* @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
*/
public function testNonClassNameStringValues()
{
$testedValue = 'error@example.com';
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory($loader, $cache);
$cache
->expects($this->never())
->method('read');
$factory->getMetadataFor($testedValue);
}

public function testMetadataCacheWithRuntimeConstraint()
{
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
Expand Down

0 comments on commit 0f9c45e

Please sign in to comment.