1
0
mirror of synced 2025-02-19 21:43:20 +03:00

support for arrays with inline JSON, which resulted in better support for aliexpress.postproduct.redefining.categoryforecast response data

This commit is contained in:
Pavel 2020-10-02 12:37:56 +03:00
parent 294211417d
commit e0790363a9
4 changed files with 139 additions and 13 deletions

View File

@ -19,6 +19,7 @@ use JMS\Serializer\Exception\RuntimeException;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use JsonException;
use SplStack;
/**
@ -201,12 +202,35 @@ class JsonDeserializationVisitor extends AbstractVisitor implements Deserializat
));
}
$data[$metadata->serializedName] = json_decode(
$data[$metadata->serializedName],
true,
512,
JSON_THROW_ON_ERROR
);
try {
if ('array' === $metadata->type['name'] && is_array($data[$metadata->serializedName])) {
$newList = [];
foreach ($data[$metadata->serializedName] as $key => $item) {
$newList[$key] = json_decode(
$item,
true,
512,
JSON_THROW_ON_ERROR
);
}
$data[$metadata->serializedName] = $newList;
} else {
$data[$metadata->serializedName] = json_decode(
$data[$metadata->serializedName],
true,
512,
JSON_THROW_ON_ERROR
);
}
} catch (JsonException $exception) {
if (in_array('InlineJsonBodyNullIfInvalid', $metadata->groups ?? [])) {
$data[$metadata->serializedName] = null;
} else {
throw $exception;
}
}
}
if (true === $metadata->inline) {

View File

@ -0,0 +1,52 @@
<?php
/**
* PHP version 7.4
*
* @category CategorySuitabilityDto
* @package RetailCrm\Model\Response\AliExpress\Result
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Model\Response\AliExpress\Result;
use JMS\Serializer\Annotation as JMS;
/**
* Class CategorySuitabilityDto
*
* @category CategorySuitabilityDto
* @package RetailCrm\Model\Response\AliExpress\Result
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class CategorySuitabilityDto
{
/**
* @var float $score
*
* @JMS\Type("float")
* @JMS\SerializedName("score")
*/
public $score;
/**
* @var float $score
*
* @JMS\Type("float")
* @JMS\SerializedName("suitabilityRank")
*/
public $suitabilityRank;
/**
* @var int $categoryId
*
* @JMS\Type("int")
* @JMS\SerializedName("categoryId")
*/
public $categoryId;
}

View File

@ -27,10 +27,11 @@ use JMS\Serializer\Annotation as JMS;
class CategorySuitabilityList
{
/**
* @var string[] $json
* @var \RetailCrm\Model\Response\AliExpress\Result\CategorySuitabilityDto[] $json
*
* @JMS\Type("array<string>")
* @JMS\Type("array<RetailCrm\Model\Response\AliExpress\Result\CategorySuitabilityDto>")
* @JMS\SerializedName("json")
* @JMS\Groups({"InlineJsonBody", "InlineJsonBodyNullIfInvalid"})
*/
public $json;
}

View File

@ -172,7 +172,7 @@ EOF;
}
}
public function testClientAliexpressPostproductRedefiningCategoryForecast()
public function testClientAliexpressPostproductRedefiningCategoryForecastEmpty()
{
$json = <<<'EOF'
{
@ -220,15 +220,64 @@ EOF;
"The result of dii is empty. It should have a correct JSON format data return.",
$response->responseData->result->errorMessage
);
self::assertEquals(
['N/A'],
$response->responseData->result->categorySuitabilityList->json
);
self::assertNull($response->responseData->result->categorySuitabilityList->json);
self::assertEquals('20181101111211', $response->responseData->result->timeStamp);
self::assertEquals('24000011', $response->responseData->result->errorCode);
self::assertTrue($response->responseData->result->success);
}
public function testClientAliexpressPostproductRedefiningCategoryForecast()
{
$json = <<<'EOF'
{
"aliexpress_postproduct_redefining_categoryforecast_response": {
"result": {
"category_suitability_list": {
"json": [
"{\"score\":0.696,\"suitabilityRank\":1,\"categoryId\":200000346}"
]
},
"success": true,
"time_stamp": "2019-07-15 13:49:58"
},
"request_id": "10ixzzbmna198"
}
}
EOF;
$mock = self::getMockClient();
$mock->on(
RequestMatcher::createMatcher('api.taobao.com')
->setPath('/router/rest')
->setOptionalQueryParams([
'app_key' => self::getEnvAppKey(),
'method' => 'aliexpress.postproduct.redefining.categoryforecast',
'session' => self::getEnvToken()
]),
$this->responseJson(200, $json)
);
$client = ClientBuilder::create()
->setContainer($this->getContainer($mock))
->setAppData($this->getEnvAppData())
->setAuthenticator($this->getEnvTokenAuthenticator())
->build();
$request = new PostproductRedefiningCategoryForecast();
$request->subject = 'man t-shirt';
$request->locale = 'en';
/** @var PostproductRedefiningCategoryForecastResponse $response */
$response = $client->sendAuthenticatedRequest($request);
$items = $response->responseData->result->categorySuitabilityList->json;
self::assertCount(1, $items);
$item = $response->responseData->result->categorySuitabilityList->json[0];
self::assertEquals(0.696, $item->score);
self::assertEquals(1, $item->suitabilityRank);
self::assertEquals(200000346, $item->categoryId);
}
public function testClientAliexpressSolutionFeedSubmit()
{
$json = <<<'EOF'