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

ChangeAssetName with assetKey doesn't work. #1720

Closed heshamMassoud closed 6 years ago

heshamMassoud commented 6 years ago

I have a ChangeAssetName update action, created as follows:

ChangeAssetName.ofAssetKeyAndVariantId(1, "assetKey", LocalizedString.ofEnglish("newAssetName"), true));

Now this update action, in JSON, looks as follows:

    {
      "action": "changeAssetName",
      "staged": true,
      "variantId": 1,
      "name": {
        "en": "newAssetName"
      },
      "assetKey": "assetKey"
    }

Which should be valid to update the asset on our API.

However, issuing such an update action using the JVM SDK, leads to the following exception:

io.sphere.sdk.client.ErrorResponseException: detailMessage: Either 'assetId' or 'assetKey' must be defined
summary: POST https://api.sphere.io/java-sync-target-dev1/products/f374e87c-09f0-4888-8f06-6e58110a2ba7 failed  with response code 400 with X-Correlation-ID `java-sync-target-dev1/dfcd3a71-cd47-4bdb-9570-754f7c6ba964` on 2018-02-28T10:54:35.845Z
http response formatted body: {
  "statusCode" : 400,
  "message" : "Either 'assetId' or 'assetKey' must be defined",
  "errors" : [ {
    "code" : "InvalidInput",
    "message" : "Either 'assetId' or 'assetKey' must be defined",
    "action" : {
      "action" : "changeAssetName",
      "variantId" : 1,
      "staged" : true,
      "name" : {
        "en" : "newAssetName"
      }
    },
    "actionIndex" : 1
  } ]
}
request formatted body: {
  "version" : 1,
  "actions" : [ {
    "action" : "changeAssetName",
    "staged" : true,
    "variantId" : 1,
    "name" : {
      "en" : "newAssetName"
    }
  }]
}
additional notes: []
Javadoc: http://commercetools.github.io/commercetools-jvm-sdk/apidocs/io/sphere/sdk/client/ErrorResponseException.html

I debugged why this happens in the JVM SDK, apparently it's in the MetaModelUpdateCommandDslImpl#httpRequestIntent where the POJO update actions are parsed into JSON, specifically in this line

toJsonString(new UpdateCommandBody<>(getVersioned().getVersion(), getUpdateActions()))

This actually leads to the aforementioned update action to be serialised to:

    {
      "action": "changeAssetName",
      "staged": true,
      "variantId": 1,
      "name": {
        "en": "newAssetName"
      }
    }

As you can see without the key which leads to the error from the platform.

heshamMassoud commented 6 years ago

I think the problem is in the missing getAssetKey() in the ChangeAssetName class, that's why probably the Jackson JSON mapper doesn't serialise the key for this update action. 😉

heshamMassoud commented 6 years ago

Also for SetAssetCustomField

katmatt commented 6 years ago

Hi @heshamMassoud ,

Thanks a lot for reporting this and creating the PR 👍 We will take a look at this issue and your PR this week.