Skip to content

Commit

Permalink
Merge pull request #799 from Bouwdie/handle-xsd-nil-as-one
Browse files Browse the repository at this point in the history
Evaluate XML xsi:nil="1" to null
  • Loading branch information
goetas authored Jul 24, 2017
2 parents 57ca3c4 + d6991f0 commit fe455ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/JMS/Serializer/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,9 @@ public function isNull($value)
$xsiAttributes = $value->attributes('http://www.w3.org/2001/XMLSchema-instance');

//We have to keep the isset quiet, some tests give error: `Node no longer exists`; even though it evaluates to false
if (@isset($xsiAttributes['nil']) && (string) $xsiAttributes['nil'] === 'true') {
if (@isset($xsiAttributes['nil'])
&& ((string) $xsiAttributes['nil'] === 'true' || (string) $xsiAttributes['nil'] === '1')
) {
return true;
}
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Serializer/XmlSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,11 @@ public function testEvaluatesToNull()
{
$namingStrategy = $this->getMockBuilder(PropertyNamingStrategyInterface::class)->getMock();
$visitor = new XmlDeserializationVisitor($namingStrategy);
$element = simplexml_load_string('<empty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>');
$xsdNilAsTrueElement = simplexml_load_string('<empty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>');
$xsdNilAsOneElement = simplexml_load_string('<empty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1"/>');

$this->assertTrue($visitor->isNull($element));
$this->assertTrue($visitor->isNull($xsdNilAsTrueElement));
$this->assertTrue($visitor->isNull($xsdNilAsOneElement));
$this->assertTrue($visitor->isNull(null));
}

Expand Down

0 comments on commit fe455ee

Please sign in to comment.