Skip to content

Commit 7988043

Browse files
committed
configure objectMapper to ignore unknown properties
1 parent cf984c0 commit 7988043

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/common/configuration/ObjectMapperConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.nhs.adaptors.gp2gp.common.configuration;
22

33
import com.fasterxml.jackson.core.StreamReadConstraints;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import org.springframework.context.annotation.Bean;
67
import org.springframework.context.annotation.Configuration;
@@ -13,6 +14,7 @@ public ObjectMapper objectMapper() {
1314
ObjectMapper mapper = new ObjectMapper();
1415
mapper.getFactory().setStreamReadConstraints(
1516
StreamReadConstraints.builder().maxStringLength(Integer.MAX_VALUE).build());
17+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1618
return mapper;
1719
}
1820
}

service/src/test/java/uk/nhs/adaptors/gp2gp/common/configuration/ObjectMapperConfigTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package uk.nhs.adaptors.gp2gp.common.configuration;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import com.fasterxml.jackson.core.StreamReadConstraints;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import org.junit.jupiter.api.Test;
67
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
78

9+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
810
import static org.junit.jupiter.api.Assertions.assertEquals;
911

1012
class ObjectMapperConfigTest {
@@ -24,4 +26,25 @@ void objectMapperConfigSetToWorkWithUnlimitedDataSizeTest() {
2426
context.close();
2527
}
2628

29+
@Test
30+
public void objectMapperConfigIgnoresUnknownPropertiesTest() {
31+
32+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ObjectMapperConfig.class);
33+
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
34+
35+
String jsonWithUnknownProperty = "{\"knownProperty\":\"testValue\", \"unknownProperty\":\"extraValue\"}";
36+
37+
assertDoesNotThrow(() -> {
38+
TestClass result = objectMapper.readValue(jsonWithUnknownProperty, TestClass.class);
39+
assertEquals("testValue", result.knownProperty);
40+
});
41+
42+
context.close();
43+
}
44+
45+
public static class TestClass {
46+
@JsonProperty("knownProperty")
47+
private String knownProperty;
48+
}
49+
2750
}

0 commit comments

Comments
 (0)