diff --git a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java index b5b81fa4a3..9c065bcc7c 100644 --- a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java +++ b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/SubTypeHandlingUnitTest.java @@ -1,14 +1,22 @@ package com.baeldung.jackson.inheritance; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.junit.Test; -import java.util.List; -import java.util.ArrayList; -import java.io.IOException; - +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Car; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Fleet; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Truck; +import com.baeldung.jackson.inheritance.SubTypeConstructorStructure.Vehicle; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; +import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; public class SubTypeHandlingUnitTest { @Test @@ -23,21 +31,30 @@ public class SubTypeHandlingUnitTest { } @Test - public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException { + public void givenSubType_whenNotUsingNoArgsConstructors_thenSucceed() throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.enableDefaultTyping(); + PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder() + .allowIfSubType("com.baeldung.jackson.inheritance") + .allowIfSubType("java.util.ArrayList") + .build(); + mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); + + Car car = new Car("Mercedes-Benz", "S500", 5, 250.0); + Truck truck = new Truck("Isuzu", "NQR", 7500.0); - SubTypeConstructorStructure.Car car = new SubTypeConstructorStructure.Car("Mercedes-Benz", "S500", 5, 250.0); - SubTypeConstructorStructure.Truck truck = new SubTypeConstructorStructure.Truck("Isuzu", "NQR", 7500.0); - - List vehicles = new ArrayList<>(); + List vehicles = new ArrayList<>(); vehicles.add(car); vehicles.add(truck); - SubTypeConstructorStructure.Fleet serializedFleet = new SubTypeConstructorStructure.Fleet(); + Fleet serializedFleet = new Fleet(); serializedFleet.setVehicles(vehicles); String jsonDataString = mapper.writeValueAsString(serializedFleet); - mapper.readValue(jsonDataString, SubTypeConstructorStructure.Fleet.class); + mapper.readValue(jsonDataString, Fleet.class); + + Fleet deserializedFleet = mapper.readValue(jsonDataString, Fleet.class); + + assertThat(deserializedFleet.getVehicles().get(0), instanceOf(Car.class)); + assertThat(deserializedFleet.getVehicles().get(1), instanceOf(Truck.class)); } } \ No newline at end of file diff --git a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java index 02297b9ee8..ca057edadc 100644 --- a/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java +++ b/jackson-modules/jackson/src/test/java/com/baeldung/jackson/inheritance/TypeInfoInclusionUnitTest.java @@ -10,12 +10,18 @@ import java.util.ArrayList; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator; +import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; public class TypeInfoInclusionUnitTest { @Test public void givenTypeInfo_whenAnnotatingGlobally_thenTypesAreCorrectlyRecovered() throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.enableDefaultTyping(); + PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder() + .allowIfSubType("com.baeldung.jackson.inheritance") + .allowIfSubType("java.util.ArrayList") + .build(); + mapper.activateDefaultTyping(ptv, ObjectMapper.DefaultTyping.NON_FINAL); TypeInfoStructure.Car car = new TypeInfoStructure.Car("Mercedes-Benz", "S500", 5, 250.0); TypeInfoStructure.Truck truck = new TypeInfoStructure.Truck("Isuzu", "NQR", 7500.0);