diff --git a/src/Serializers/QueryResultSerializer.php b/src/Serializers/QueryResultSerializer.php index 8aa57cecf7..ecba7d30cd 100644 --- a/src/Serializers/QueryResultSerializer.php +++ b/src/Serializers/QueryResultSerializer.php @@ -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; @@ -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 = [ @@ -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(); diff --git a/tests/phpunit/Integration/JSONScript/TestCases/s-0032.json b/tests/phpunit/Integration/JSONScript/TestCases/s-0032.json new file mode 100644 index 0000000000..dd2f11adf2 --- /dev/null +++ b/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 + } +}