File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed
main/java/uk/nhs/adaptors/gp2gp/common/configuration
test/java/uk/nhs/adaptors/gp2gp/common/configuration Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change 11package uk .nhs .adaptors .gp2gp .common .configuration ;
22
33import com .fasterxml .jackson .core .StreamReadConstraints ;
4+ import com .fasterxml .jackson .databind .DeserializationFeature ;
45import com .fasterxml .jackson .databind .ObjectMapper ;
56import org .springframework .context .annotation .Bean ;
67import 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}
Original file line number Diff line number Diff line change 11package uk .nhs .adaptors .gp2gp .common .configuration ;
22
3+ import com .fasterxml .jackson .annotation .JsonProperty ;
34import com .fasterxml .jackson .core .StreamReadConstraints ;
45import com .fasterxml .jackson .databind .ObjectMapper ;
56import org .junit .jupiter .api .Test ;
67import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
78
9+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
810import static org .junit .jupiter .api .Assertions .assertEquals ;
911
1012class 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}
You can’t perform that action at this time.
0 commit comments