Skip to content

Commit 0f9c45e

Browse files
bug #26823 [Validator] Fix LazyLoadingMetadataFactory with PSR6Cache 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
2 parents 16edba5 + 5198f43 commit 0f9c45e

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Symfony/Component/Validator/Mapping/Factory/LazyLoadingMetadataFactory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ public function getMetadataFor($value)
9090
return $this->loadedClasses[$class];
9191
}
9292

93+
if (!class_exists($class) && !interface_exists($class, false)) {
94+
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
95+
}
96+
9397
if (null !== $this->cache && false !== ($metadata = $this->cache->read($class))) {
9498
// Include constraints from the parent class
9599
$this->mergeConstraints($metadata);
96100

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

100-
if (!class_exists($class) && !interface_exists($class)) {
101-
throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class));
102-
}
103-
104104
$metadata = new ClassMetadata($class);
105105

106106
if (null !== $this->loader) {

src/Symfony/Component/Validator/Tests/Mapping/Factory/LazyLoadingMetadataFactoryTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ public function testReadMetadataFromCache()
149149
$this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS));
150150
}
151151

152+
/**
153+
* @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
154+
*/
155+
public function testNonClassNameStringValues()
156+
{
157+
$testedValue = 'error@example.com';
158+
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
159+
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
160+
$factory = new LazyLoadingMetadataFactory($loader, $cache);
161+
$cache
162+
->expects($this->never())
163+
->method('read');
164+
$factory->getMetadataFor($testedValue);
165+
}
166+
152167
public function testMetadataCacheWithRuntimeConstraint()
153168
{
154169
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();

0 commit comments

Comments
 (0)