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 authored and phil-davis committed Apr 18, 2024
1 parent 43cea9f commit 775c2a8
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -223,6 +223,9 @@ function valueObject(Reader $reader, string $className, string $namespace): obje
// 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
37 changes: 37 additions & 0 deletions tests/Sabre/Xml/Deserializer/ValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ public function testDeserializeValueObjectIgnoredElement(): void
);
}

public function testDeserializeValueObjectIgnoredNamespace(): void
{
$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(): void
{
$input = <<<XML
Expand Down

0 comments on commit 775c2a8

Please sign in to comment.