fix: parse createProduct response as JsonNode to avoid multilingual field mismatch

PS WebService POST response wraps multilingual fields as {"language":[...]}
while GET returns a flat array. Jackson fails parsing List<LangValue> from
the POST response. Fix: parse the full response as JsonNode and extract
only the id; reconstruct the DTO from the input fields.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
luklpz 2026-05-14 19:58:40 +02:00
parent 997ed99895
commit 4469d7dd7e
1 changed files with 18 additions and 4 deletions

View File

@ -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}
* <p>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.</p>
*
* @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. */