fix: discard PS POST response body, fetch created product via GET
PS WebService POST response uses a different JSON structure for multilingual fields than GET responses, causing Jackson deserialization failures. Instead of parsing the POST body, discard it with toBodilessEntity() and fetch the created product via getProductByReference() which uses the stable GET format. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
961a1b8a8a
commit
359590b7d8
|
|
@ -3,7 +3,6 @@ package com.teterialosjuanjos.tfg.sync_service.integration.prestashop;
|
||||||
import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties;
|
import com.teterialosjuanjos.tfg.sync_service.config.IntegrationProperties;
|
||||||
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*;
|
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.dto.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
@ -86,19 +85,18 @@ public class PrestashopClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new product in PrestaShop.
|
* Creates a new product in PrestaShop and returns it with its assigned PS id.
|
||||||
* PS WebService requires XML request body even when {@code output_format=JSON} is set.
|
|
||||||
*
|
*
|
||||||
* <p>The POST response JSON wraps multilingual fields differently from GET
|
* <p>PS WebService requires XML request body even when {@code output_format=JSON} is set.
|
||||||
* (e.g. {@code {"language":[...]}} instead of a flat array), so the response
|
* The POST response JSON wraps multilingual fields in a different structure than GET
|
||||||
* is parsed as a raw {@link JsonNode} and only the assigned {@code id} is extracted.
|
* responses, so the response body is discarded and a subsequent GET by reference is used
|
||||||
* All other fields are taken from the input DTO.</p>
|
* to retrieve the created product with its id.</p>
|
||||||
*
|
*
|
||||||
* @return the created product with its assigned PS {@code id}
|
* @return the created product with its assigned PS {@code id}, or {@code null} if not found after creation
|
||||||
*/
|
*/
|
||||||
public PrestashopProductDto createProduct(PrestashopProductDto dto) {
|
public PrestashopProductDto createProduct(PrestashopProductDto dto) {
|
||||||
log.debug("Creating PrestaShop product reference={}", dto.reference());
|
log.debug("Creating PrestaShop product reference={}", dto.reference());
|
||||||
JsonNode response = restClient.post()
|
restClient.post()
|
||||||
.uri(u -> u.path("/products")
|
.uri(u -> u.path("/products")
|
||||||
.queryParam("ws_key", wsKey)
|
.queryParam("ws_key", wsKey)
|
||||||
.queryParam("output_format", "JSON")
|
.queryParam("output_format", "JSON")
|
||||||
|
|
@ -106,17 +104,9 @@ public class PrestashopClient {
|
||||||
.contentType(MediaType.APPLICATION_XML)
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
.body(toProductXml(dto).getBytes(StandardCharsets.UTF_8))
|
.body(toProductXml(dto).getBytes(StandardCharsets.UTF_8))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.body(JsonNode.class);
|
.toBodilessEntity();
|
||||||
|
|
||||||
Integer psId = null;
|
return getProductByReference(dto.reference()).orElse(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. */
|
/** Updates an existing product by its PrestaShop internal ID. */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue