Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f262b01

Browse files
committed
bug #11672 [Routing] fix handling of nullable XML attributes (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Routing] fix handling of nullable XML attributes | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | As @Tobion pointed out in #11394, ``true`` and ``1`` are valid values in boolean XML attributes. The XmlFileLoader didn't handle ``1`` values properly. Commits ------- 7b4d4b6 fix handling of nullable XML attributes
2 parents 00aedfc + 7b4d4b6 commit f262b01

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private function parseConfigs(\DOMElement $node, $path)
215215
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
216216
switch ($n->localName) {
217217
case 'default':
218-
if ($n->hasAttribute('xsi:nil') && 'true' == $n->getAttribute('xsi:nil')) {
218+
if ($this->isElementValueNull($n)) {
219219
$defaults[$n->getAttribute('key')] = null;
220220
} else {
221221
$defaults[$n->getAttribute('key')] = trim($n->textContent);
@@ -235,4 +235,15 @@ private function parseConfigs(\DOMElement $node, $path)
235235

236236
return array($defaults, $requirements, $options);
237237
}
238+
239+
private function isElementValueNull(\DOMElement $element)
240+
{
241+
$namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
242+
243+
if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
244+
return false;
245+
}
246+
247+
return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
248+
}
238249
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<routes xmlns="http://symfony.com/schema/routing"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
5+
6+
<route id="blog_show" path="/blog/{slug}">
7+
<default key="foo" xsi:nil="true" />
8+
<default key="bar" xsi:nil="1" />
9+
<default key="foobar" xsi:nil="false">foo</default>
10+
<default key="baz" xsi:nil="0">bar</default>
11+
</route>
12+
</routes>

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,18 @@ public function testDocTypeIsNotAllowed()
124124
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
125125
$loader->load('withdoctype.xml');
126126
}
127+
128+
public function testNullValues()
129+
{
130+
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
131+
$routeCollection = $loader->load('null_values.xml');
132+
$route = $routeCollection->get('blog_show');
133+
134+
$this->assertTrue($route->hasDefault('foo'));
135+
$this->assertNull($route->getDefault('foo'));
136+
$this->assertTrue($route->hasDefault('bar'));
137+
$this->assertNull($route->getDefault('bar'));
138+
$this->assertEquals('foo', $route->getDefault('foobar'));
139+
$this->assertEquals('bar', $route->getDefault('baz'));
140+
}
127141
}

0 commit comments

Comments
 (0)