commercetools / commercetools-sdk-java-v2

The e-commerce SDK from commercetools for Java.
https://commercetools.github.io/commercetools-sdk-java-v2/javadoc/index.html
Apache License 2.0
34 stars 15 forks source link

get productSelections for a product by key doesn't work #254

Closed andrei-ivanov closed 2 years ago

andrei-ivanov commented 2 years ago
List<ProductSelection> productSelections = projectApiRoot.products().withKey(productKey).productSelections().get().executeBlocking().getBody().getResults();

results in

INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : listing selections for product iphone-13
DEBUG --- [ForkJoinPool.commonPool-worker-1]                       commercetools.products.request.queries : io.vrap.rmf.base.client.ApiHttpRequest@7fe3477b[method=GET,uri="https://api.europe-west1.gcp.commercetools.com/loylogic-poc/products/key=iphone-13/product-selections",headers=[{key=Accept-Encoding, value=gzip}, {key=Authorization, value=**removed from output**}, {key=User-Agent, value=commercetools-sdk-java-v2/7.6.0  Java/17.0.2+8-LTS (Windows 10; amd64)}],textInterpretedBody=empty body]
TRACE --- [ForkJoinPool.commonPool-worker-1]                       commercetools.products.request.queries : GET https://api.europe-west1.gcp.commercetools.com/loylogic-poc/products/key=iphone-13/product-selections <no body>
INFO  --- [    httpclient-dispatch-2]                              commercetools.products.response : GET https://api.europe-west1.gcp.commercetools.com/loylogic-poc/products/key=iphone-13/product-selections 200
DEBUG --- [    httpclient-dispatch-2]                              commercetools.products.response : io.vrap.rmf.base.client.ApiHttpResponse@5eff0dc7[statusCode=200,headers=[{key=date, value=Wed, 02 Feb 2022 10:39:20 GMT}, {key=server, value=istio-envoy}, {key=x-envoy-upstream-service-time, value=75}, {key=content-encoding, value=gzip}, {key=access-control-allow-headers, value=Accept, Authorization, Content-Type, Origin, User-Agent, X-Correlation-ID}, {key=x-correlation-id, value=projects-ba85f082-58ce-40c4-ba75-6f927b03ac81}, {key=access-control-allow-methods, value=GET, POST, DELETE, OPTIONS}, {key=via, value=1.1 google}, {key=access-control-expose-headers, value=X-Correlation-ID}, {key=access-control-allow-origin, value=*}, {key=access-control-max-age, value=299}, {key=content-type, value=application/json; charset=utf-8}, {key=server-timing, value=projects;dur=7}, {key=alt-svc, value=clear}],textInterpretedBody={"limit":20,"offset":0,"count":1,"results":[{"productSelection":{"typeId":"product-selection","id":"2e89d89b-bdfb-4339-8e53-7638966a97c2"},"createdAt":"2022-02-02T09:15:35.001Z"}]}]
TRACE --- [    httpclient-dispatch-2]                              commercetools.products.response : 200
{
  "limit" : 20,
  "offset" : 0,
  "count" : 1,
  "results" : [ {
    "productSelection" : {
      "typeId" : "product-selection",
      "id" : "2e89d89b-bdfb-4339-8e53-7638966a97c2"
    },
    "createdAt" : "2022-02-02T09:15:35.001Z"
  } ]
}
INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : results: 1
INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : ProductSelectionImpl[createdAt=2022-02-02T09:15:35.001Z,createdBy=<null>,id=<null>,key=<null>,lastModifiedAt=<null>,lastModifiedBy=<null>,name=<null>,productCount=<null>,type=<null>,version=<null>]

As can be seen, the createdAt property gets initialized, but the others not, like id, since there seems to be a productSelection wrapper in the JSON.

Stack information:

Same as the JS counterpart

jenschude commented 2 years ago

It's a wrong definition in an upstream repository. Will be updated soon.

Thanks for the report

jenschude commented 2 years ago

Hi could you please try the 7.5.0-SNAPSHOT from this location? https://oss.sonatype.org/content/repositories/snapshots/com/commercetools/sdk/commercetools-sdk-java-api/7.5.0-SNAPSHOT/

andrei-ivanov commented 2 years ago

Seems to be working fine... The API changed a bit for the result type:

List<AssignedProductSelection> productSelections = projectApiRoot.products().withKey(productKey).productSelections().get().executeBlocking().getBody().getResults();

Btw, how do I expand that obj?

INFO  --- [    httpclient-dispatch-2]                              commercetools.products.response : GET https://api.europe-west1.gcp.commercetools.com/loylogic-poc/products/key=iphone-13/product-selections 200
INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : results: 2
INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : AssignedProductSelectionImpl[createdAt=2022-02-02T09:15:35.001Z,productSelection=ProductSelectionReferenceImpl[id=2e89d89b-bdfb-4339-8e53-7638966a97c2,obj=<null>,typeId=product-selection]]
INFO  --- [                     main] com.loylogic.experimental.commercetools.ProductSelectionsService : AssignedProductSelectionImpl[createdAt=2022-02-02T11:44:33.841Z,productSelection=ProductSelectionReferenceImpl[id=21a7ebc9-75e4-4be4-b3e7-a9808796ac1c,obj=<null>,typeId=product-selection]]
jenschude commented 2 years ago

For expansion please use projectApiRoot.products().withKey(productKey).productSelections().get().withExpand("productSelection")

And yes the API had to change as it was documented wrong and the implementation was using the documentation :)

andrei-ivanov commented 2 years ago

Got it, works great with the expansion too. Waiting for new releases now 😁

Thanks for the fixes

jenschude commented 2 years ago

Released 8.0.0-beta.1 as a pre-release with the fixed return type

jenschude commented 2 years ago

Fixed with 8.0.0

Thanks for the report