提交 77abe078 编写于 作者: S Sebastien Deleuze

Set Jackson DEFAULT_VIEW_INCLUSION property to false by default

Issue: SPR-12179
上级 42aef5f5
......@@ -47,6 +47,12 @@ import org.springframework.util.ClassUtils;
/**
* A builder used to create {@link ObjectMapper} instances with a fluent API.
*
* <p>It customizes Jackson defaults properties with the following ones:
* <ul>
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
* </ul>
*
* <p>Note that Jackson's JSR-310 and Joda-Time support modules will be registered automatically
* when available (and when Java 8 and Joda-Time themselves are available, respectively).
*
......@@ -384,6 +390,9 @@ public class Jackson2ObjectMapperBuilder {
this.objectMapper.registerModule(module);
}
if(!features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
configureFeature(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
......
......@@ -52,6 +52,12 @@ import org.springframework.util.ClassUtils;
* {@link XmlMapper} ({@code createXmlMapper} property set to true) with setters
* to enable or disable Jackson features from within XML configuration.
*
* <p>It customizes Jackson defaults properties with the following ones:
* <ul>
* <li>{@link MapperFeature#DEFAULT_VIEW_INCLUSION} is disabled</li>
* <li>{@link DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES} is disabled</li>
* </ul>
*
* <p>Example usage with
* {@link MappingJackson2HttpMessageConverter}:
*
......@@ -413,6 +419,9 @@ public class Jackson2ObjectMapperFactoryBean implements FactoryBean<ObjectMapper
this.objectMapper.registerModule(module);
}
if(!features.containsKey(MapperFeature.DEFAULT_VIEW_INCLUSION)) {
configureFeature(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
}
if(!features.containsKey(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
configureFeature(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
......
......@@ -80,7 +80,7 @@ public class Jackson2ObjectMapperBuilderTests {
public void defaultProperties() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
assertNotNull(objectMapper);
assertTrue(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertTrue(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
......@@ -240,7 +240,7 @@ public class Jackson2ObjectMapperBuilderTests {
assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
......
......@@ -259,7 +259,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
assertTrue(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS));
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
......
......@@ -255,7 +255,7 @@ public class MappingJackson2HttpMessageConverterTests {
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, containsString("\"withoutView\":\"without\""));
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
}
@Test
......@@ -288,7 +288,7 @@ public class MappingJackson2HttpMessageConverterTests {
assertThat(result, endsWith(");"));
assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, containsString("\"withoutView\":\"without\""));
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
}
......
......@@ -126,7 +126,7 @@ public class MappingJackson2XmlHttpMessageConverterTests {
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
assertThat(result, containsString("<withView1>with</withView1>"));
assertThat(result, not(containsString("<withView2>with</withView2>")));
assertThat(result, containsString("<withoutView>without</withoutView>"));
assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
@Test
......
......@@ -226,7 +226,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
assertTrue(s.contains("\"with1\":\"with\""));
assertFalse(s.contains("\"with2\":\"with\""));
assertTrue(s.contains("\"without\":\"without\""));
assertFalse(s.contains("\"without\":\"without\""));
}
// SPR-12123
......
......@@ -173,8 +173,8 @@ public class MvcNamespaceTests {
for(HttpMessageConverter<?> converter : converters) {
if(converter instanceof AbstractJackson2HttpMessageConverter) {
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertEquals(XmlMapper.class, objectMapper.getClass());
......
......@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportExtensionTests {
assertEquals(1, adapter.getMessageConverters().size());
assertEquals(MappingJackson2HttpMessageConverter.class, adapter.getMessageConverters().get(0).getClass());
ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)adapter.getMessageConverters().get(0)).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
......
......@@ -162,8 +162,8 @@ public class WebMvcConfigurationSupportTests {
for(HttpMessageConverter<?> converter : converters) {
if(converter instanceof AbstractJackson2HttpMessageConverter) {
ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
assertTrue(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertTrue(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
if(converter instanceof MappingJackson2XmlHttpMessageConverter) {
assertEquals(XmlMapper.class, objectMapper.getClass());
......
......@@ -322,7 +322,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\""));
assertTrue(content.contains("\"withoutView\":\"without\""));
assertFalse(content.contains("\"withoutView\":\"without\""));
}
@Test
......@@ -343,7 +343,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("\"withView1\":\"with\""));
assertTrue(content.contains("\"withView2\":\"with\""));
assertTrue(content.contains("\"withoutView\":\"without\""));
assertFalse(content.contains("\"withoutView\":\"without\""));
}
// SPR-12149
......@@ -366,7 +366,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("<withView1>with</withView1>"));
assertTrue(content.contains("<withView2>with</withView2>"));
assertTrue(content.contains("<withoutView>without</withoutView>"));
assertFalse(content.contains("<withoutView>without</withoutView>"));
}
// SPR-12149
......@@ -389,7 +389,7 @@ public class RequestResponseBodyMethodProcessorTests {
String content = this.servletResponse.getContentAsString();
assertFalse(content.contains("<withView1>with</withView1>"));
assertTrue(content.contains("<withView2>with</withView2>"));
assertTrue(content.contains("<withoutView>without</withoutView>"));
assertFalse(content.contains("<withoutView>without</withoutView>"));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册