commercetools / commercetools-sync-java

Java library for importing and syncing (taking care of changes) data into one or more commercetools projects from external data files or from another commercetools project.
https://commercetools.github.io/commercetools-sync-java
Apache License 2.0
32 stars 37 forks source link

Products with product references are not synced #1127

Closed dfrommi closed 8 months ago

dfrommi commented 9 months ago

Describe the bug Products referencing other products are not synced. They are skipped due to unknown references.

To Reproduce Steps to reproduce the behavior:

  1. Call ProductSync.sync() for a product that references other products
  2. Changes are not synchronised

Expected behavior References to other products should be resolved and the changes synchronised. A matching by product-key is expected. (The bug is not about changes in referenced products. The input-product is not synchronised.)

Additional context Analysis-result so far:

  1. Product-references are collected in ProductBatchValidator.getReferencedKeysWithReferenceTypeId().
  2. The id of the products are returned, instead of the key
  3. BaseService.cacheKeysToIdsUsingGraphQl() filters for keys on the target-project, so it doesn't find the referenced products and thus they are not available in cache
  4. This leads to missingReferencedProductKeys to be non-empty in ProductSync.syncOrKeepTrack() which prevents the sync

What in my opinion needs to be done:

salander85 commented 9 months ago

Hi @dfrommi , thank you for reporting this issue. We will check it and let you know the results.

To help us reproducing your issue please let us know

Regards, Sarah

dfrommi commented 9 months ago

Hello Sarah,

thanks for your answer. We're trying to use the latest version as of now, which is 10.0.2, as we have to migrate to SDK v2 before end of the year.

Unfortunately I can't post products into publicly available issues. But you can create any attribute which references a product, like this:

image

Hope this is good enough, otherwise I can maybe share project-key and product-id of an example, if you'd have access to them. But I'd have to clarify first, so I hope that's enough.

salander85 commented 9 months ago

Hi @dfrommi , thanks for your response. The posted information should be good enough. Only one question i need to know is, are the referenced products already existing in your target project when you try to sync? Regards.

dfrommi commented 9 months ago

Hello @salander85 Yes they are and also published.

salander85 commented 9 months ago

Hi @dfrommi , i've tested your scenario and figured out the problems you are facing result from a misconfiguration of the ProductDraft you want to sync. Please have a look in the docs about the ProductSync. In the last row you can see, that attributes of type SetType with elementType is ReferenceType are required to have the KEY set in the id-field. Otherwise the sync considers the referenced product as existing and skips reference resolution by key.

If your source-project is another commercetools project you can consider to use ProductTransformUtils to avoid this problem. Please let me know when you are already using the transform-utils and still facing the issue.

I hope this answer will help to fix your problem. Let me know when you need further support.

dfrommi commented 8 months ago

Thanks you so much for your quick help! On a first quick test, it updates something. Have to look closer into it, but looks promising as of now.

And yes, the ProductTransformUtils-method did the trick. We were using ProductReferenceResolutionUtils.mapToProductDrafts from the SDK instead, which is not resolving keys.

dfrommi commented 8 months ago

I've done more tests and the problem is resolved. Thank you very much, @salander85

To create the ProductDraft, use com.commercetools.sync.products.utils.ProductTransformUtils#toProductDrafts.

(Don't use ProductReferenceResolutionUtils.mapToProductDrafts from the CT SDK)