diff --git a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java index f624a0b..54bffa7 100644 --- a/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java +++ b/sync-service/src/main/java/com/teterialosjuanjos/tfg/sync_service/integration/prestashop/PrestashopClient.java @@ -3,6 +3,7 @@ package com.teterialosjuanjos.tfg.sync_service.integration.prestashop; import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties; import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*; import lombok.extern.slf4j.Slf4j; +import com.fasterxml.jackson.databind.JsonNode; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; @@ -88,11 +89,16 @@ public class PrestashopClient { * Creates a new product in PrestaShop. * PS WebService requires XML request body even when {@code output_format=JSON} is set. * - * @return the created product including its assigned {@code id} + *
The POST response JSON wraps multilingual fields differently from GET + * (e.g. {@code {"language":[...]}} instead of a flat array), so the response + * is parsed as a raw {@link JsonNode} and only the assigned {@code id} is extracted. + * All other fields are taken from the input DTO.
+ * + * @return the created product with its assigned PS {@code id} */ public PrestashopProductDto createProduct(PrestashopProductDto dto) { log.debug("Creating PrestaShop product reference={}", dto.reference()); - PrestashopProductDto.SingleResponse response = restClient.post() + JsonNode response = restClient.post() .uri(u -> u.path("/products") .queryParam("ws_key", wsKey) .queryParam("output_format", "JSON") @@ -100,9 +106,17 @@ public class PrestashopClient { .contentType(MediaType.APPLICATION_XML) .body(toProductXml(dto).getBytes(StandardCharsets.UTF_8)) .retrieve() - .body(PrestashopProductDto.SingleResponse.class); + .body(JsonNode.class); - return response != null ? response.product() : null; + Integer psId = null; + if (response != null && response.has("product")) { + JsonNode product = response.get("product"); + if (product.has("id")) { + psId = product.get("id").asInt(); + } + } + return new PrestashopProductDto(psId, dto.reference(), dto.price(), dto.active(), + dto.idCategoryDefault(), dto.name(), dto.description(), dto.descriptionShort()); } /** Updates an existing product by its PrestaShop internal ID. */