Skip to content

Commit

Permalink
Merge pull request #746 from commercetools/fix-deserialize-subtypes
Browse files Browse the repository at this point in the history
Fix deserialization if subtype discriminator is missing
  • Loading branch information
jenschude authored Oct 18, 2024
2 parents fe6974d + 4f1f258 commit 0621fc2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.commercetools.api.models.cart_discount.*;
import com.commercetools.api.models.common.ReferenceTypeId;

import io.vrap.rmf.base.client.utils.json.JsonUtils;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

public class CartDiscountTest {
Expand Down Expand Up @@ -94,4 +98,17 @@ public void giftLineItemValueToDraft() {
});

}

@Test
public void deserializeGiftLineItemWithoutTypeId() {
String json = "{\"value\": { \"type\": \"giftLineItem\", \"product\": { \"id\": \"foo\" } } }";
CartDiscount cartDiscount = JsonUtils.fromJsonString(json, CartDiscount.class);
Assertions.assertThat(cartDiscount.getValue())
.isInstanceOfSatisfying(CartDiscountValueGiftLineItem.class, cartDiscountValueGiftLineItem -> {
assertThat(cartDiscountValueGiftLineItem.getProduct().getId()).isEqualTo("foo");
assertThat(cartDiscountValueGiftLineItem.getProduct().getTypeId())
.isEqualTo(ReferenceTypeId.PRODUCT);
});
Assertions.assertThat(cartDiscount.getValue()).isInstanceOf(CartDiscountValueGiftLineItem.class);
}
}
2 changes: 1 addition & 1 deletion gradle-scripts/extensions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ext {
convenience: 'com.commercetools.sdk.jvm.core:commercetools-convenience:2.16.0',
version: {
strictly '[1.62.0,)'
prefer '2.14.0'
prefer '2.16.0'
}
]

Expand Down
14 changes: 7 additions & 7 deletions licenses/commercetools-sdk-compat-v1/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": [
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -12,7 +12,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client-ahc-2_0",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -22,7 +22,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client-core",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -32,7 +32,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-models",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -42,7 +42,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-sdk-base",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -52,7 +52,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:sdk-http",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -62,7 +62,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:sdk-http-ahc-2_0",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand Down
14 changes: 7 additions & 7 deletions licenses/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": [
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -12,7 +12,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client-ahc-2_0",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -22,7 +22,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-java-client-core",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -32,7 +32,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-models",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -42,7 +42,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:commercetools-sdk-base",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -52,7 +52,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:sdk-http",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand All @@ -62,7 +62,7 @@
},
{
"moduleName": "com.commercetools.sdk.jvm.core:sdk-http-ahc-2_0",
"moduleVersion": "2.14.0",
"moduleVersion": "2.16.0",
"moduleLicenses": [
{
"moduleLicense": "Apache License, Version 2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -52,17 +53,18 @@ public static ObjectMapper createObjectMapper(final ModuleOptions options) {
final List<SimpleModule> moduleList = new ArrayList<>();
suppliers.iterator().forEachRemaining(moduleSupplier -> moduleList.add(moduleSupplier.getModule(options)));

final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()) //provides serialization and deserialization for LocalDate and LocalTime (JSR310 Jackson module)
.registerModule(new ZonedDateTimeSerializationModule()) //custom serializer for LocalDate, LocalTime and ZonedDateTime
.registerModule(new ZonedDateTimeDeserializationModule()) //custom deserializer for ZonedDateTime
.registerModule(new LocalDateDeserializationModule()) //custom deserializer for LocalDate
.registerModules(loader)
.registerModules(moduleList)
.setSerializationInclusion(JsonInclude.Include.NON_NULL) //ignore null fields
return JsonMapper.builder()
.addModule(new JavaTimeModule()) //provides serialization and deserialization for LocalDate and LocalTime (JSR310 Jackson module)
.addModule(new ZonedDateTimeSerializationModule()) //custom serializer for LocalDate, LocalTime and ZonedDateTime
.addModule(new ZonedDateTimeDeserializationModule()) //custom deserializer for ZonedDateTime
.addModule(new LocalDateDeserializationModule()) //custom deserializer for LocalDate
.addModules(loader)
.addModules(moduleList)
.serializationInclusion(JsonInclude.Include.NON_NULL) //ignore null fields
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return objectMapper;
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(MapperFeature.REQUIRE_TYPE_ID_FOR_SUBTYPES, false)
.build();
}

/**
Expand Down

0 comments on commit 0621fc2

Please sign in to comment.