Skip to content

Commit

Permalink
Fixed handling of elements from foreign namespaces in values object
Browse files Browse the repository at this point in the history
When encountering an element from another namespace the value object parser
does skip the opening element. However when it encounters the closing element
it did handle it like it was the closing element of the one being processed.

This commit and unit test fixes the issue by ignoring the element completely
  • Loading branch information
mrbig committed Feb 8, 2024
1 parent 17f1042 commit a476c68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Deserializer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ function valueObject(Reader $reader, string $className, string $namespace)
// Ignore property
$reader->next();
}
} elseif (Reader::ELEMENT === $reader->nodeType) {
// Skipping element from different namespace
$reader->next();
} else {
if (Reader::END_ELEMENT !== $reader->nodeType && !$reader->read()) {
break;
Expand Down
38 changes: 38 additions & 0 deletions tests/Sabre/Xml/Deserializer/ValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ public function testDeserializeValueObjectIgnoredElement()
);
}

public function testDeserializeValueObjectIgnoredNamespace()
{
$input = <<<XML
<?xml version="1.0"?>
<foo xmlns="urn:foo" xmlns:alien="urn:example.com">
<firstName>Harry</firstName>
<alien:email>[email protected]</alien:email>
<lastName>Turtle</lastName>
</foo>
XML;

$reader = new Reader();
$reader->xml($input);
$reader->elementMap = [
'{urn:foo}foo' => function (Reader $reader) {
return valueObject($reader, 'Sabre\\Xml\\Deserializer\\TestVo', 'urn:foo');
},
];

$output = $reader->parse();

$vo = new TestVo();
$vo->firstName = 'Harry';
$vo->lastName = 'Turtle';

$expected = [
'name' => '{urn:foo}foo',
'value' => $vo,
'attributes' => [],
];

$this->assertEquals(
$expected,
$output
);
}


public function testDeserializeValueObjectAutoArray()
{
$input = <<<XML
Expand Down

0 comments on commit a476c68

Please sign in to comment.