amzn / selling-partner-api-models

This repository contains OpenAPI models for developers to use when developing software to call Selling Partner APIs.
Apache License 2.0
601 stars 730 forks source link

[FEATURE] suggest catalogItems_2020-12-01.json change attributes type to object #195

Closed ihoment-meihui closed 2 years ago

ihoment-meihui commented 2 years ago

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 image

chapmanjw commented 2 years ago

The attributes schema is a reference that already defines this as an object with additional properties:

https://github.com/amzn/selling-partner-api-models/blob/main/models/catalog-items-api-model/catalogItems_2020-12-01.json#L1078-L1082

    "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.

chapmanjw commented 2 years ago

@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.

chapmanjw commented 2 years ago

@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.

ihoment-meihui commented 2 years ago

@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. image

ihoment-meihui commented 2 years ago

I expect the code generated will look like image Or image

ihoment-meihui commented 2 years ago

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.

image

Thanks for your advice.

chapmanjw commented 2 years ago

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!