Browse Source

Merge branch '5.1.x'

pull/22705/head
Sebastien Deleuze 6 years ago
parent
commit
864dddee7a
  1. 22
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  2. 12
      spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

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

@ -614,8 +614,8 @@ public class Jackson2ObjectMapperBuilder { @@ -614,8 +614,8 @@ public class Jackson2ObjectMapperBuilder {
ObjectMapper mapper;
if (this.createXmlMapper) {
mapper = (this.defaultUseWrapper != null ?
new XmlObjectMapperInitializer().create(this.defaultUseWrapper) :
new XmlObjectMapperInitializer().create());
new XmlObjectMapperInitializer().create(this.defaultUseWrapper, this.factory) :
new XmlObjectMapperInitializer().create(this.factory));
}
else {
mapper = (this.factory != null ? new ObjectMapper(this.factory) : new ObjectMapper());
@ -839,14 +839,24 @@ public class Jackson2ObjectMapperBuilder { @@ -839,14 +839,24 @@ public class Jackson2ObjectMapperBuilder {
private static class XmlObjectMapperInitializer {
public ObjectMapper create() {
return new XmlMapper(StaxUtils.createDefensiveInputFactory());
public ObjectMapper create(@Nullable JsonFactory factory) {
if (factory != null) {
return new XmlMapper((XmlFactory) factory);
}
else {
return new XmlMapper(StaxUtils.createDefensiveInputFactory());
}
}
public ObjectMapper create(boolean defaultUseWrapper) {
public ObjectMapper create(boolean defaultUseWrapper, @Nullable JsonFactory factory) {
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(defaultUseWrapper);
return new XmlMapper(new XmlFactory(StaxUtils.createDefensiveInputFactory()), module);
if (factory != null) {
return new XmlMapper((XmlFactory) factory, module);
}
else {
return new XmlMapper(new XmlFactory(StaxUtils.createDefensiveInputFactory()), module);
}
}
}

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

@ -71,6 +71,7 @@ import com.fasterxml.jackson.databind.ser.std.NumberSerializer; @@ -71,6 +71,7 @@ import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import kotlin.ranges.IntRange;
@ -480,6 +481,14 @@ public class Jackson2ObjectMapperBuilderTests { @@ -480,6 +481,14 @@ public class Jackson2ObjectMapperBuilderTests {
assertEquals(XmlMapper.class, objectMapper.getClass());
}
@Test // gh-22428
public void xmlMapperAndCustomFactory() {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().factory(new MyXmlFactory()).build();
assertNotNull(objectMapper);
assertEquals(XmlMapper.class, objectMapper.getClass());
assertEquals(MyXmlFactory.class, objectMapper.getFactory().getClass());
}
@Test
public void createXmlMapper() {
Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().indentOutput(true);
@ -667,4 +676,7 @@ public class Jackson2ObjectMapperBuilderTests { @@ -667,4 +676,7 @@ public class Jackson2ObjectMapperBuilderTests {
}
public static class MyXmlFactory extends XmlFactory {
}
}

Loading…
Cancel
Save