Skip to content

Commit

Permalink
QueryResultSerializer to handle _qty on chained property, refs 3517 (
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames authored and kghbln committed Jan 18, 2019
1 parent 4d9e5a7 commit b17526d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Serializers/QueryResultSerializer.php
Expand Up @@ -101,7 +101,17 @@ public static function getSerialization( DataItem $dataItem, $printRequest = nul
];

foreach ( $recordValue->getDataItem()->getSemanticData()->getPropertyValues( $property ) as $value ) {
$recordDiValues[$label]['item'][] = self::getSerialization( $value );

if ( $property->findPropertyTypeID() === '_qty' ) {
$dataValue = DataValueFactory::getInstance()->newDataValueByItem( $value, $property );

$recordDiValues[$label]['item'][] = [
'value' => $dataValue->getNumber(),
'unit' => $dataValue->getUnit()
];
} else {
$recordDiValues[$label]['item'][] = self::getSerialization( $value );
}
}
}
$result = $recordDiValues;
Expand Down Expand Up @@ -130,6 +140,11 @@ public static function getSerialization( DataItem $dataItem, $printRequest = nul
// (unit is part of the datavalue object)
if ( $printRequest !== null && $printRequest->getTypeID() === '_qty' ) {
$diProperty = $printRequest->getData()->getDataItem();

if ( $printRequest->isMode( \SMW\Query\PrintRequest::PRINT_CHAIN ) ) {
$diProperty = $printRequest->getData()->getLastPropertyChainValue()->getDataItem();
}

$dataValue = DataValueFactory::getInstance()->newDataValueByItem( $dataItem, $diProperty );

$result = [
Expand Down Expand Up @@ -259,8 +274,12 @@ private static function serialize_printrequest( $printRequest ) {
return $serialized;
}

$serialized['redi'] = '';

// To match forwarded redirects
$serialized['redi'] = !$data->getInceptiveProperty()->equals( $data->getDataItem() ) ? $data->getInceptiveProperty()->getKey() : '';
if ( !$data->getInceptiveProperty()->equals( $data->getDataItem() ) ) {
$serialized['redi'] = $data->getInceptiveProperty()->getKey();
}

// To match internal properties like _MDAT
$serialized['key'] = $data->getDataItem()->getKey();
Expand Down
65 changes: 65 additions & 0 deletions tests/phpunit/Integration/JSONScript/TestCases/s-0032.json
@@ -0,0 +1,65 @@
{
"description": "Test `format=json` output via `Special:Ask` for `_ref_rec`/`_qty` type (#3517)",
"setup": [
{
"namespace": "SMW_NS_PROPERTY",
"page": "Ref prop",
"contents": "[[Has type::Reference]] [[Has fields::Field prop;other prop]]"
},
{
"namespace": "SMW_NS_PROPERTY",
"page": "Field prop",
"contents": "[[Has type::Quantity]] [[Corresponds to::1 km]]"
},
{
"namespace": "NS_MAIN",
"page": "S0032/1",
"contents": "[[Ref prop::123;S0032/2]] [[Category:S0032]]"
}
],
"tests": [
{
"type": "special",
"about": "#0",
"special-page": {
"page": "Ask",
"request-parameters": {
"p": {
"link": "none",
"limit": "10",
"offset": "0",
"mainlabel": "",
"prettyprint": false,
"unescape": true,
"format": "json"
},
"q": "[[Category:S0032]]",
"po": "?Ref prop.Field prop"
}
},
"assert-output": {
"to-contain": [
"\"chain\":\"Ref prop.Field prop\"",
"{\"Field prop\":[{\"value\":123,\"unit\":\"km\"}]}"
]
}
}
],
"settings": {
"wgContLang": "en",
"wgLang": "en",
"wgLanguageCode": "en",
"smwgPageSpecialProperties": [
"_MDAT"
],
"smwgNamespacesWithSemanticLinks": {
"NS_MAIN": true,
"SMW_NS_PROPERTY": true
}
},
"meta": {
"version": "2",
"is-incomplete": false,
"debug": false
}
}

0 comments on commit b17526d

Please sign in to comment.