commercetools / commercetools-jvm-sdk

The e-commerce SDK from commercetools running on the Java virtual machine.
https://commercetools.github.io/commercetools-jvm-sdk/apidocs/index.html
Other
62 stars 40 forks source link

Add Product to ProductSelection InvalidJsonInput #2228

Closed jclaramo closed 1 year ago

jclaramo commented 1 year ago

Describe the bug When using the UpdateAction AddProduct instead of "product" it creates a "resourceIdentifierProduct" which it's not match with the API.

To Reproduce

ProductSelection productSelection = sphereClient.execute(ProductSelectionByKeyGet
                .of(productSelectionKey))
                .toCompletableFuture()
                .join();

        ResourceIdentifier<Product> resourceIdentifier = ResourceIdentifier.ofKey(productKey, "product");

        AddProduct addProduct = AddProduct.of(resourceIdentifier);

        ProductSelectionUpdateCommand updateCommand = ProductSelectionUpdateCommand
                .of(productSelection,
                        addProduct);

        try {
            blockingSphereClient.executeBlocking(updateCommand);
        }catch (ErrorResponseException ex){
            System.out.println(ex.getMessage());
        }

This is the json request body that crashes;

{
  "version" : {{productSelectionVersion}},
  "actions" : [ {
    "action" : "addProduct",
    "productResourceIdentifier" : {
      "key" : "{{key}}",
      "typeId" : "product"
    }
  } ]
}

Expected behavior

{
  "version" : {{productSelectionVersion}},
  "actions" : [ {
    "action" : "addProduct",
    "product" : {
      "key" : "{{key}}",
      "typeId" : "product"
    }
  } ]
}

Stack information (please complete the following information):

Additional context image

barbara79 commented 1 year ago

Hello, I checked with your code and it works for me. In our api documentation, the property product takes the ResourceIdentifier of the Product. You can find same behavior in our JavaDoc

I would suggest you check if the API specification is correctly updated on your side.

Which SDK version do you have? I cannot find the 11.0.0

jclaramo commented 1 year ago

Hello, I messed writing the SDK version, the SDK version that I am using the latest version 2.10.0.

jclaramo commented 1 year ago

Sorry, I missclicked and closed the issue.

barbara79 commented 1 year ago

so because the behavior is right, and it should work with the product Resource Identifier and it looks like it doesn't take the right API, could you please send us the exact error you are receiving?

jclaramo commented 1 year ago

The error is ErrorResponseException, the full error message is:

detailMessage: Request body does not contain valid JSON.
summary: POST https://api.europe-west1.gcp.commercetools.com/product-selections/f3a16f3e-3d49-4319-8a93-bc37220f8419 failed  with response code 400 with X-Correlation-ID `{{project-key}}/7b986149-ac8b-4225-bd6a-f8e45a4af48a` on 2023-01-04T12:58:07.376942900Z
http response formatted body: {
  "statusCode" : 400,
  "message" : "Request body does not contain valid JSON.",
  "errors" : [ {
    "code" : "InvalidJsonInput",
    "message" : "Request body does not contain valid JSON.",
    "detailedErrorMessage" : "actions -> product: Missing required value"
  } ]
}
http response: io.sphere.sdk.http.HttpResponseImpl@37052337[statusCode=400,headers={date=[Wed, 04 Jan 2023 12:58:03 GMT], content-length=[172], server=[istio-envoy], x-envoy-upstream-service-time=[3], access-control-allow-headers=[Accept, Authorization, Content-Type, Origin, User-Agent, X-Correlation-ID], content-encoding=[gzip], x-correlation-id=[{{project-key}}/7b986149-ac8b-4225-bd6a-f8e45a4af48a], access-control-allow-methods=[GET, POST, DELETE, OPTIONS], access-control-expose-headers=[X-Correlation-ID], via=[1.1 google], access-control-allow-origin=[*], x-http-status-caused-by-external-upstream=[false], access-control-max-age=[299], content-type=[application/json; charset=utf-8], server-timing=[projects;dur=2], alt-svc=[h3=":443"; ma=2592000,h3-29=":443"; ma=2592000]},associatedRequest=<null>,textInterpretedBody={"statusCode":400,"message":"Request body does not contain valid JSON.","errors":[{"code":"InvalidJsonInput","message":"Request body does not contain valid JSON.","detailedErrorMessage":"actions -> product: Missing required value"}]}]
SDK: 2.9.0
project: {{project-key}}
endpoint: POST /product-selections/f3a16f3e-3d49-4319-8a93-bc37220f8419
Java: 11.0.13
cwd: C:\Users\{{user}}\Desktop\Personal\Java\{{project-key}}
sphere request: ProductSelectionUpdateCommandImpl[additionalHttpQueryParameters=[],baseEndpointWithoutId=/product-selections,creationFunction=io.sphere.sdk.productselections.commands.ProductSelectionUpdateCommandImpl$$Lambda$271/0x000000080030e440@6f2cfcc2,expansionModel=ProductSelectionExpansionModelImpl[pathExpressions=[]],expansionPaths=[],javaType=[simple type, class io.sphere.sdk.productselections.ProductSelection],updateActions=[AddProduct[productResourceIdentifier=ResourceIdentifierImpl[id=<null>,key=51860,typeId=product],action=addProduct]],versioned=ProductSelectionImpl[createdAt=2023-01-03T08:00:38.695Z,createdBy=CreatedByImpl[anonymousId=<null>,clientId=<null>,customer=<null>,externalUserId=<null>],custom=<null>,id=f3a16f3e-3d49-4319-8a93-bc37220f8419,key=eurosur-products,lastModifiedAt=2023-01-04T09:17:45.138Z,lastModifiedBy=LastModifiedByImpl[anonymousId=<null>,clientId=Xem4Cf0iFbZFM5xCsSRCDcrn,customer=<null>,externalUserId=<null>],name=LocalizedString(en -> , es -> Productos Eurosur),productCount=1,type=INDIVIDUAL,version=2]]
http request: HttpRequestImpl[body=io.sphere.sdk.http.StringHttpRequestBody@3ecd267f[body={"version":2,"actions":[{"action":"addProduct","productResourceIdentifier":{"key":"51860","typeId":"product"}}]}],headers={},httpMethod=POST,url=https://api.europe-west1.gcp.commercetools.com/product-selections/f3a16f3e-3d49-4319-8a93-bc37220f8419]
http request formatted body: {
  "version" : 2,
  "actions" : [ {
    "action" : "addProduct",
    "productResourceIdentifier" : {
      "key" : "51860",
      "typeId" : "product"
    }
  } ]
}
additional notes: []
Javadoc: https://commercetools.github.io/commercetools-jvm-sdk/apidocs/io/sphere/sdk/client/ErrorResponseException.html

Thanks.

barbara79 commented 1 year ago

So from what I can see, the error is in the request, would you mind trying it this way, please? AddProduct addProduct = AddProduct.of(product.toResourceIdentifier()); and then pass this result in the request like you did before.

jclaramo commented 1 year ago

I tried and it keeps sending the same json wrong body with the "productResourceIdentifier" instead of "product".

barbara79 commented 1 year ago

I opened a PR for this problem. I'll let you know once we will issue a new release.

jclaramo commented 1 year ago

Ok, thank you very much.

jenschude commented 1 year ago

I just released version 2.11.0 with the fix