Skip to content

Commit 042279e

Browse files
committed
Merge pull request #27659 from lower-case
* pr/27659: Polish "Add property to customize Jackson's default leniency" Add property to customize Jackson's default leniency Closes gh-27659
2 parents ead8305 + 68a47a7 commit 042279e

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public void customize(Jackson2ObjectMapperBuilder builder) {
187187
configurePropertyNamingStrategy(builder);
188188
configureModules(builder);
189189
configureLocale(builder);
190+
configureDefaultLeniency(builder);
190191
}
191192

192193
private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) {
@@ -289,6 +290,13 @@ private void configureLocale(Jackson2ObjectMapperBuilder builder) {
289290
}
290291
}
291292

293+
private void configureDefaultLeniency(Jackson2ObjectMapperBuilder builder) {
294+
Boolean defaultLeniency = this.jacksonProperties.getDefaultLeniency();
295+
if (defaultLeniency != null) {
296+
builder.postConfigurer((objectMapper) -> objectMapper.setDefaultLeniency(defaultLeniency));
297+
}
298+
}
299+
292300
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory, Class<T> type) {
293301
return BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, type).values();
294302
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,6 +92,11 @@ public class JacksonProperties {
9292
*/
9393
private JsonInclude.Include defaultPropertyInclusion;
9494

95+
/**
96+
* Global default setting (if any) for leniency.
97+
*/
98+
private Boolean defaultLeniency;
99+
95100
/**
96101
* Time zone used when formatting dates. For instance, "America/Los_Angeles" or
97102
* "GMT+10".
@@ -151,6 +156,14 @@ public void setDefaultPropertyInclusion(JsonInclude.Include defaultPropertyInclu
151156
this.defaultPropertyInclusion = defaultPropertyInclusion;
152157
}
153158

159+
public Boolean getDefaultLeniency() {
160+
return this.defaultLeniency;
161+
}
162+
163+
public void setDefaultLeniency(Boolean defaultLeniency) {
164+
this.defaultLeniency = defaultLeniency;
165+
}
166+
154167
public TimeZone getTimeZone() {
155168
return this.timeZone;
156169
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626

2727
import com.fasterxml.jackson.annotation.JsonCreator;
2828
import com.fasterxml.jackson.annotation.JsonCreator.Mode;
29+
import com.fasterxml.jackson.annotation.JsonFormat;
2930
import com.fasterxml.jackson.annotation.JsonInclude;
3031
import com.fasterxml.jackson.core.JsonGenerator;
3132
import com.fasterxml.jackson.core.JsonParser;
@@ -40,6 +41,7 @@
4041
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
4142
import com.fasterxml.jackson.databind.SerializationFeature;
4243
import com.fasterxml.jackson.databind.SerializerProvider;
44+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
4345
import com.fasterxml.jackson.databind.module.SimpleModule;
4446
import com.fasterxml.jackson.databind.util.StdDateFormat;
4547
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
@@ -57,6 +59,7 @@
5759
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
5860

5961
import static org.assertj.core.api.Assertions.assertThat;
62+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6063
import static org.mockito.Mockito.mock;
6164

6265
/**
@@ -301,6 +304,25 @@ void customTimeZoneFormattingADate() {
301304
});
302305
}
303306

307+
@Test
308+
void enableDefaultLeniency() {
309+
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:true").run((context) -> {
310+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
311+
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
312+
assertThat(person.getBirthDate()).isNotNull();
313+
});
314+
}
315+
316+
@Test
317+
void disableDefaultLeniency() {
318+
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> {
319+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
320+
assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
321+
.isInstanceOf(InvalidFormatException.class).hasMessageContaining("expected format")
322+
.hasMessageContaining("yyyyMMdd");
323+
});
324+
}
325+
304326
@Test
305327
void additionalJacksonBuilderCustomization() {
306328
this.contextRunner.withUserConfiguration(ObjectMapperBuilderCustomConfig.class).run((context) -> {
@@ -537,4 +559,19 @@ String getProperty3() {
537559

538560
}
539561

562+
static class Person {
563+
564+
@JsonFormat(pattern = "yyyyMMdd")
565+
private Date birthDate;
566+
567+
Date getBirthDate() {
568+
return this.birthDate;
569+
}
570+
571+
void setBirthDate(Date birthDate) {
572+
this.birthDate = birthDate;
573+
}
574+
575+
}
576+
540577
}

0 commit comments

Comments
 (0)