diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index d43bb686ff..a1318c4519 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -187,7 +187,7 @@ public class JacksonAutoConfiguration { configurePropertyNamingStrategy(builder); configureModules(builder); configureLocale(builder); - configureLeniency(builder); + configureDefaultLeniency(builder); } private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map 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 Collection getBeans(ListableBeanFactory beanFactory, Class type) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java index 46c4d2ced5..fa9e17e912 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java @@ -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; - } - } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java index 10ddd64271..3d2856d576 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java @@ -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; }