Add property to customize Jackson's default leniency

See gh-27659
This commit is contained in:
Lovekesh Garg
2021-08-14 23:12:15 +05:30
committed by Stephane Nicoll
parent ead8305596
commit 89c532ab70
3 changed files with 70 additions and 0 deletions

View File

@@ -187,6 +187,7 @@ public class JacksonAutoConfiguration {
configurePropertyNamingStrategy(builder);
configureModules(builder);
configureLocale(builder);
configureLeniency(builder);
}
private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) {
@@ -289,6 +290,11 @@ public class JacksonAutoConfiguration {
}
}
private void configureLeniency(Jackson2ObjectMapperBuilder builder) {
Boolean lenient = this.jacksonProperties.getLenient();
builder.postConfigurer(objectMapper -> objectMapper.setDefaultLeniency(lenient));
}
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory, Class<T> type) {
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type).values();
}

View File

@@ -103,6 +103,11 @@ public class JacksonProperties {
*/
private Locale locale;
/**
* Setting for leniency, in case of absence it will be considered lenient = true;
*/
private Boolean lenient;
public String getDateFormat() {
return this.dateFormat;
}
@@ -167,4 +172,12 @@ public class JacksonProperties {
this.locale = locale;
}
public Boolean getLenient() {
return lenient;
}
public void setLenient(Boolean lenient) {
this.lenient = lenient;
}
}

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -40,6 +41,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
@@ -301,6 +303,40 @@ class JacksonAutoConfigurationTests {
});
}
@Test
void disableLeniency() {
this.contextRunner.withPropertyValues("spring.jackson.lenient:false").run((context) -> {
boolean invalidFormat = false;
ObjectMapper mapper = context.getBean(ObjectMapper.class);
try {
mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
}
catch (InvalidFormatException e) {
assertThat(e).isNotNull();
invalidFormat = true;
}
assertThat(invalidFormat).isTrue();
});
}
@Test
void enableLeniency() {
this.contextRunner.withPropertyValues("spring.jackson.lenient:true").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
assertThat(person.getBirthDate()).isNotNull();
});
}
@Test
void defaultLeniency() {
this.contextRunner.run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
assertThat(person.getBirthDate()).isNotNull();
});
}
@Test
void additionalJacksonBuilderCustomization() {
this.contextRunner.withUserConfiguration(ObjectMapperBuilderCustomConfig.class).run((context) -> {
@@ -537,4 +573,19 @@ class JacksonAutoConfigurationTests {
}
static class Person {
@JsonFormat(pattern = "yyyyMMdd")
private Date birthDate;
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}
}