diff --git a/src/Component/JMS/Visitor/Deserialization/JsonDeserializationVisitor.php b/src/Component/JMS/Visitor/Deserialization/JsonDeserializationVisitor.php index ec1cd45..cb0a5c9 100644 --- a/src/Component/JMS/Visitor/Deserialization/JsonDeserializationVisitor.php +++ b/src/Component/JMS/Visitor/Deserialization/JsonDeserializationVisitor.php @@ -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) { diff --git a/src/Model/Response/AliExpress/Result/CategorySuitabilityDto.php b/src/Model/Response/AliExpress/Result/CategorySuitabilityDto.php new file mode 100644 index 0000000..7a53acc --- /dev/null +++ b/src/Model/Response/AliExpress/Result/CategorySuitabilityDto.php @@ -0,0 +1,52 @@ + + * @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 + * @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; +} diff --git a/src/Model/Response/AliExpress/Result/Entity/CategorySuitabilityList.php b/src/Model/Response/AliExpress/Result/Entity/CategorySuitabilityList.php index 775e0fb..ffd263c 100644 --- a/src/Model/Response/AliExpress/Result/Entity/CategorySuitabilityList.php +++ b/src/Model/Response/AliExpress/Result/Entity/CategorySuitabilityList.php @@ -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") + * @JMS\Type("array") * @JMS\SerializedName("json") + * @JMS\Groups({"InlineJsonBody", "InlineJsonBodyNullIfInvalid"}) */ public $json; } diff --git a/tests/RetailCrm/Tests/TopClient/ClientTest.php b/tests/RetailCrm/Tests/TopClient/ClientTest.php index 919798f..00a6919 100644 --- a/tests/RetailCrm/Tests/TopClient/ClientTest.php +++ b/tests/RetailCrm/Tests/TopClient/ClientTest.php @@ -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'