Polish "Add property to customize Jackson's default leniency"

See gh-27659
This commit is contained in:
Stephane Nicoll
2021-08-16 08:56:26 +02:00
parent 89c532ab70
commit 68a47a7f11
3 changed files with 32 additions and 44 deletions

View File

@@ -187,7 +187,7 @@ public class JacksonAutoConfiguration {
configurePropertyNamingStrategy(builder);
configureModules(builder);
configureLocale(builder);
configureLeniency(builder);
configureDefaultLeniency(builder);
}
private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) {
@@ -290,9 +290,11 @@ public class JacksonAutoConfiguration {
}
}
private void configureLeniency(Jackson2ObjectMapperBuilder builder) {
Boolean lenient = this.jacksonProperties.getLenient();
builder.postConfigurer(objectMapper -> objectMapper.setDefaultLeniency(lenient));
private void configureDefaultLeniency(Jackson2ObjectMapperBuilder builder) {
Boolean defaultLeniency = this.jacksonProperties.getDefaultLeniency();
if (defaultLeniency != null) {
builder.postConfigurer((objectMapper) -> objectMapper.setDefaultLeniency(defaultLeniency));
}
}
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory, Class<T> type) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -92,6 +92,11 @@ public class JacksonProperties {
*/
private JsonInclude.Include defaultPropertyInclusion;
/**
* Global default setting (if any) for leniency.
*/
private Boolean defaultLeniency;
/**
* Time zone used when formatting dates. For instance, "America/Los_Angeles" or
* "GMT+10".
@@ -103,11 +108,6 @@ 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;
}
@@ -156,6 +156,14 @@ public class JacksonProperties {
this.defaultPropertyInclusion = defaultPropertyInclusion;
}
public Boolean getDefaultLeniency() {
return this.defaultLeniency;
}
public void setDefaultLeniency(Boolean defaultLeniency) {
this.defaultLeniency = defaultLeniency;
}
public TimeZone getTimeZone() {
return this.timeZone;
}
@@ -172,12 +180,4 @@ public class JacksonProperties {
this.locale = locale;
}
public Boolean getLenient() {
return lenient;
}
public void setLenient(Boolean lenient) {
this.lenient = lenient;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@ import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
/**
@@ -304,24 +305,8 @@ 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) -> {
void enableDefaultLeniency() {
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:true").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
assertThat(person.getBirthDate()).isNotNull();
@@ -329,11 +314,12 @@ class JacksonAutoConfigurationTests {
}
@Test
void defaultLeniency() {
this.contextRunner.run((context) -> {
void disableDefaultLeniency() {
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> {
ObjectMapper mapper = context.getBean(ObjectMapper.class);
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
assertThat(person.getBirthDate()).isNotNull();
assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
.isInstanceOf(InvalidFormatException.class).hasMessageContaining("expected format")
.hasMessageContaining("yyyyMMdd");
});
}
@@ -578,11 +564,11 @@ class JacksonAutoConfigurationTests {
@JsonFormat(pattern = "yyyyMMdd")
private Date birthDate;
public Date getBirthDate() {
return birthDate;
Date getBirthDate() {
return this.birthDate;
}
public void setBirthDate(Date birthDate) {
void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}