Closed ihoment-meihui closed 2 years ago
The attributes
schema is a reference that already defines this as an object with additional properties:
"ItemAttributes": {
"description": "A JSON object that contains structured item attribute data keyed by attribute name. Catalog item attributes are available only to brand owners and conform to the related product type definitions available in the Selling Partner API for Product Type Definitions.",
"additionalProperties": true,
"type": "object"
},
Nesting that shouldn't have a material difference here.
Swagger codegen does not play well with Swagger 2.0 models and additionalProperties
. For our own testing and such, we use the generated client to make the "raw" call and handle the JSON directly.
Call call = catalogApi.getCatalogItemCall(asin, marketplaces, includedDataList, null, null, null);
Response response = call.execute();
ResponseBody responseBody = response.body();
byte[] responseBytes = responseBody.bytes();
And then use your favorite JSON handler for reading the bytes as JSON (such as using the Jackson ObjectMapper to convert to JsonNode).
I'll also share this Github issue with the team that handles the client generation code and sample to see if there is an alternative here.
@ihoment-meihui Another option you can use here is to override the mapping of the ItemAttributes
object to the generic JSON object of the serializer you are using with the Swagger codegen.
For example, with GSON:
--import-mappings ItemAttributes=com.google.gson.JsonObject
Or with Jackson:
--import-mappings ItemAttributes=com.fasterxml.jackson.databind.node.ObjectNode
This results in the attributes
property of the generated code using one of these generic JSON objects instead of the Java Object
type.
@ihoment-meihui Depending on how fancy you want to get with a solution here, you could also generate POJOs from the JSON Schema from the Product Type Definitions API and then use the Import Mappings to map the ItemAttributes to the POJOs generated from the JSON Schema.
@chapmanjw Thanks. I tried with GSON
--import-mappings ItemAttributes=com.google.gson.JsonObject
, but the effect did not achieve what I wanted
Here is still ItemAttributes, not JSONObject.
I expect the code generated will look like Or
I use the swagger-codegen-cli-2.4.13.jar downloaded from https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/developer-guide/SellingPartnerApiDeveloperGuide.md#generating-a-java-sdk-with-lwa-token-exchange-and-authentication.
After I replaced 'import-mapping' with 'type-mappings',
--type-mappings ItemAttributes=com.google.gson.JsonObject
I got the result I expected.
Thanks for your advice.
Going to close this issue since we have a path forward mapping to generic JSON objects. I've provided feedback to the team who handles client generation so that they can get documentation and such updated to include this. Thanks!
The Java SDK for the CatalogItem API using Swagger Code Generator cannot get (parse) attributes from SP-API response.
URL of the content on GitHub https://github.com/amzn/selling-partner-api-models/blob/main/models/catalog-items-api-model/catalogItems_2020-12-01.json
Description of what the documentation currently says "Item": { "description": "An item in the Amazon catalog.", "properties": { "asin": { "$ref": "#/definitions/ItemAsin" }, "attributes": { "$ref": "#/definitions/ItemAttributes" }, ...
Description of what the documentation should say "Item": { "description": "An item in the Amazon catalog.", "properties": { "asin": { "$ref": "#/definitions/ItemAsin" }, "attributes": { "description": "A JSON object that contains structured item attribute data keyed by attribute name. Catalog item attributes are available only to brand owners and conform to the related product type definitions available in the Selling Partner API for Product Type Definitions.", "additionalProperties": true, "type": "object" }, ...
change attributes class to Object