Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When properties appear in propstat with no value, don't call deserializers #711

Merged
merged 3 commits into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/DAV/Xml/Element/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,46 @@ function xmlSerialize(Writer $writer) {
*/
static function xmlDeserialize(Reader $reader) {

$reader->pushContext();

$reader->elementMap['{DAV:}propstat'] = 'Sabre\\Xml\\Element\\KeyValue';

// We are overriding the parser for {DAV:}prop. This deserializer is
// almost identical to the one for Sabre\Xml\Element\KeyValue.
//
// The difference is that if there are any child-elements inside of
// {DAV:}prop, that have no value, normally any deserializers are
// called. But we don't want this, because a singlular element without
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- singlular
+ singular

// child-elements implies 'no value' in {DAV:}prop, so we want to skip
// deserializers and just set null for those.
$reader->elementMap['{DAV:}prop'] = function(Reader $reader) {

if ($reader->isEmptyElement) {
$reader->next();
return [];
}
$values = [];
$reader->read();
do {
if ($reader->nodeType === Reader::ELEMENT) {
$clark = $reader->getClark();

if ($reader->isEmptyElement) {
$values[$clark] = null;
$reader->next();
} else {
$values[$clark] = $reader->parseCurrentElement()['value'];
}
} else {
$reader->read();
}
} while ($reader->nodeType !== Reader::END_ELEMENT);
$reader->read();
return $values;

};
$elems = $reader->parseInnerTree();
$reader->popContext();

$href = null;
$propertyLists = [];
Expand Down
1 change: 0 additions & 1 deletion lib/DAV/Xml/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Service extends \Sabre\Xml\Service {
public $elementMap = [
'{DAV:}multistatus' => 'Sabre\\DAV\\Xml\\Response\\MultiStatus',
'{DAV:}response' => 'Sabre\\DAV\\Xml\\Element\\Response',
'{DAV:}propstat' => 'Sabre\\Xml\\Element\\KeyValue',

// Requests
'{DAV:}propfind' => 'Sabre\\DAV\\Xml\\Request\\PropFind',
Expand Down
67 changes: 67 additions & 0 deletions tests/Sabre/DAV/Xml/Element/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,71 @@ function testSerializeBreak() {

}

function testDeserializeComplexProperty() {

$xml = '<?xml version="1.0"?>
<d:response xmlns:d="DAV:">
<d:href>/uri</d:href>
<d:propstat>
<d:prop>
<d:foo>hello</d:foo>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
';

$result = $this->parse($xml, [
'{DAV:}response' => 'Sabre\DAV\Xml\Element\Response',
'{DAV:}foo' => function($reader) {

$reader->next();
return 'world';
},
]);
$this->assertEquals(
new Response('/uri', [
'200' => [
'{DAV:}foo' => 'world',
]
]),
$result['value']
);

}

/**
* In the case of {DAV:}prop, a deserializer should never get called, if
* the property element is empty.
*/
function testDeserializeComplexPropertyEmpty() {

$xml = '<?xml version="1.0"?>
<d:response xmlns:d="DAV:">
<d:href>/uri</d:href>
<d:propstat>
<d:prop>
<d:foo />
</d:prop>
<d:status>HTTP/1.1 404 Not Found</d:status>
</d:propstat>
</d:response>
';

$result = $this->parse($xml, [
'{DAV:}response' => 'Sabre\DAV\Xml\Element\Response',
'{DAV:}foo' => function($reader) {
throw new \LogicException('This should never happen');
},
]);
$this->assertEquals(
new Response('/uri', [
'404' => [
'{DAV:}foo' => null
]
]),
$result['value']
);

}
}