Skip to content

Commit 42aef5f

Browse files
committed
Set Jackson FAIL_ON_UNKNOWN_PROPERTIES property to false by default
Issue: SPR-11891
1 parent 4e24d66 commit 42aef5f

File tree

9 files changed

+18
-13
lines changed

9 files changed

+18
-13
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ public <T extends ObjectMapper> T build() {
384384
this.objectMapper.registerModule(module);
385385
}
386386

387+
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
388+
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
389+
}
387390
for (Object feature : this.features.keySet()) {
388391
configureFeature(feature, this.features.get(feature));
389392
}

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBean.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,9 @@ public void afterPropertiesSet() {
413413
this.objectMapper.registerModule(module);
414414
}
415415

416+
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
417+
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
418+
}
416419
for (Object feature : this.features.keySet()) {
417420
configureFeature(feature, this.features.get(feature));
418421
}

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void defaultProperties() {
8181
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
8282
assertNotNull(objectMapper);
8383
assertTrue(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
84-
assertTrue(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
84+
assertFalse(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
8585
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
8686
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
8787
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_SETTERS));
@@ -241,7 +241,7 @@ public void completeSetup() {
241241

242242
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
243243
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
244-
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
244+
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
245245
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
246246
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
247247
assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperFactoryBeanTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void completeSetup() {
260260

261261
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
262262
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
263-
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
263+
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
264264
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
265265
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
266266
assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));

spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ public void readInvalidJson() throws IOException {
146146
converter.read(MyBean.class, inputMessage);
147147
}
148148

149-
@Test(expected = HttpMessageNotReadableException.class)
149+
@Test
150150
public void readValidJsonWithUnknownProperty() throws IOException {
151151
String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
152152
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
153153
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
154154
converter.read(MyBean.class, inputMessage);
155+
// Assert no HttpMessageNotReadableException is thrown
155156
}
156157

157158
@Test

spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ public void readInvalidXml() throws IOException {
102102
converter.read(MyBean.class, inputMessage);
103103
}
104104

105-
@Test(expected = HttpMessageNotReadableException.class)
105+
@Test
106106
public void readValidXmlWithUnknownProperty() throws IOException {
107107
String body = "<MyBean><string>string</string><unknownProperty>value</unknownProperty></MyBean>";
108108
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
109109
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
110110
converter.read(MyBean.class, inputMessage);
111+
// Assert no HttpMessageNotReadableException is thrown
111112
}
112113

113114
@Test

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void testDefaultConfig() throws Exception {
175175
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
176176
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
177177
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
178-
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
178+
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
179179
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
180180
assertEquals(XmlMapper.class, objectMapper.getClass());
181181
}

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportExtensionTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,9 @@ public void requestMappingHandlerAdapter() throws Exception {
162162
assertEquals(1, adapter.getMessageConverters().size());
163163
assertEquals(MappingJackson2HttpMessageConverter.class, adapter.getMessageConverters().get(0).getClass());
164164
ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)adapter.getMessageConverters().get(0)).getObjectMapper();
165-
assertTrue(objectMapper.getDeserializationConfig()
166-
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
167-
assertTrue(objectMapper.getSerializationConfig()
168-
.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
169-
assertTrue(objectMapper.getDeserializationConfig()
170-
.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
165+
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
166+
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
167+
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
171168

172169
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
173170

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void requestMappingHandlerAdapter() throws Exception {
164164
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
165165
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
166166
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
167-
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
167+
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
168168
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
169169
assertEquals(XmlMapper.class, objectMapper.getClass());
170170
}

0 commit comments

Comments
 (0)