Skip to content

Commit 68a47a7

Browse files
committed
Polish "Add property to customize Jackson's default leniency"
See gh-27659
1 parent 89c532a commit 68a47a7

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

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

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

193193
private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) {
@@ -290,9 +290,11 @@ private void configureLocale(Jackson2ObjectMapperBuilder builder) {
290290
}
291291
}
292292

293-
private void configureLeniency(Jackson2ObjectMapperBuilder builder) {
294-
Boolean lenient = this.jacksonProperties.getLenient();
295-
builder.postConfigurer(objectMapper -> objectMapper.setDefaultLeniency(lenient));
293+
private void configureDefaultLeniency(Jackson2ObjectMapperBuilder builder) {
294+
Boolean defaultLeniency = this.jacksonProperties.getDefaultLeniency();
295+
if (defaultLeniency != null) {
296+
builder.postConfigurer((objectMapper) -> objectMapper.setDefaultLeniency(defaultLeniency));
297+
}
296298
}
297299

298300
private static <T> Collection<T> getBeans(ListableBeanFactory beanFactory, Class<T> type) {

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

Lines changed: 14 additions & 14 deletions
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".
@@ -103,11 +108,6 @@ public class JacksonProperties {
103108
*/
104109
private Locale locale;
105110

106-
/**
107-
* Setting for leniency, in case of absence it will be considered lenient = true;
108-
*/
109-
private Boolean lenient;
110-
111111
public String getDateFormat() {
112112
return this.dateFormat;
113113
}
@@ -156,6 +156,14 @@ public void setDefaultPropertyInclusion(JsonInclude.Include defaultPropertyInclu
156156
this.defaultPropertyInclusion = defaultPropertyInclusion;
157157
}
158158

159+
public Boolean getDefaultLeniency() {
160+
return this.defaultLeniency;
161+
}
162+
163+
public void setDefaultLeniency(Boolean defaultLeniency) {
164+
this.defaultLeniency = defaultLeniency;
165+
}
166+
159167
public TimeZone getTimeZone() {
160168
return this.timeZone;
161169
}
@@ -172,12 +180,4 @@ public void setLocale(Locale locale) {
172180
this.locale = locale;
173181
}
174182

175-
public Boolean getLenient() {
176-
return lenient;
177-
}
178-
179-
public void setLenient(Boolean lenient) {
180-
this.lenient = lenient;
181-
}
182-
183183
}

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

Lines changed: 12 additions & 26 deletions
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.
@@ -59,6 +59,7 @@
5959
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
6060

6161
import static org.assertj.core.api.Assertions.assertThat;
62+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6263
import static org.mockito.Mockito.mock;
6364

6465
/**
@@ -304,36 +305,21 @@ void customTimeZoneFormattingADate() {
304305
}
305306

306307
@Test
307-
void disableLeniency() {
308-
this.contextRunner.withPropertyValues("spring.jackson.lenient:false").run((context) -> {
309-
boolean invalidFormat = false;
310-
ObjectMapper mapper = context.getBean(ObjectMapper.class);
311-
try {
312-
mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
313-
}
314-
catch (InvalidFormatException e) {
315-
assertThat(e).isNotNull();
316-
invalidFormat = true;
317-
}
318-
assertThat(invalidFormat).isTrue();
319-
});
320-
}
321-
322-
@Test
323-
void enableLeniency() {
324-
this.contextRunner.withPropertyValues("spring.jackson.lenient:true").run((context) -> {
308+
void enableDefaultLeniency() {
309+
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:true").run((context) -> {
325310
ObjectMapper mapper = context.getBean(ObjectMapper.class);
326311
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
327312
assertThat(person.getBirthDate()).isNotNull();
328313
});
329314
}
330315

331316
@Test
332-
void defaultLeniency() {
333-
this.contextRunner.run((context) -> {
317+
void disableDefaultLeniency() {
318+
this.contextRunner.withPropertyValues("spring.jackson.default-leniency:false").run((context) -> {
334319
ObjectMapper mapper = context.getBean(ObjectMapper.class);
335-
Person person = mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class);
336-
assertThat(person.getBirthDate()).isNotNull();
320+
assertThatThrownBy(() -> mapper.readValue("{\"birthDate\": \"2010-12-30\"}", Person.class))
321+
.isInstanceOf(InvalidFormatException.class).hasMessageContaining("expected format")
322+
.hasMessageContaining("yyyyMMdd");
337323
});
338324
}
339325

@@ -578,11 +564,11 @@ static class Person {
578564
@JsonFormat(pattern = "yyyyMMdd")
579565
private Date birthDate;
580566

581-
public Date getBirthDate() {
582-
return birthDate;
567+
Date getBirthDate() {
568+
return this.birthDate;
583569
}
584570

585-
public void setBirthDate(Date birthDate) {
571+
void setBirthDate(Date birthDate) {
586572
this.birthDate = birthDate;
587573
}
588574

0 commit comments

Comments
 (0)