Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public void customize(Jackson2ObjectMapperBuilder builder) {
configurePropertyNamingStrategy(builder);
configureModules(builder);
configureLocale(builder);
configureLeniency(builder);
}

private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) {
Expand Down Expand Up @@ -289,6 +290,11 @@ private void configureLocale(Jackson2ObjectMapperBuilder builder) {
}
}

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -167,4 +172,12 @@ public void setLocale(Locale locale) {
this.locale = locale;
}

public Boolean getLenient() {
return lenient;
}

public void setLenient(Boolean lenient) {
this.lenient = lenient;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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;
Expand All @@ -40,6 +41,7 @@
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;
Expand Down Expand Up @@ -301,6 +303,40 @@ void customTimeZoneFormattingADate() {
});
}

@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) -> {
Expand Down Expand Up @@ -537,4 +573,19 @@ String getProperty3() {

}

static class Person {

@JsonFormat(pattern = "yyyyMMdd")
private Date birthDate;

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}

}

}