From 1f63b82c5bcf6db670aacff47fe1e5dc5cffa5eb Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Wed, 9 Dec 2020 10:58:15 +0300 Subject: [PATCH 1/2] Upgrade to Jackson 2.12.0 See gh-24415 --- .../boot/autoconfigure/jackson/JacksonAutoConfiguration.java | 5 +++-- .../boot/autoconfigure/jackson/JacksonProperties.java | 2 +- .../autoconfigure/jackson/JacksonAutoConfigurationTests.java | 4 ++-- spring-boot-project/spring-boot-dependencies/build.gradle | 2 +- .../boot/buildpack/platform/json/SharedObjectMapper.java | 4 ++-- .../buildpack/platform/json/SharedObjectMapperTests.java | 5 +++-- 6 files changed, 12 insertions(+), 10 deletions(-) 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 b3e537da94..8b7327a308 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 @@ -32,6 +32,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; @@ -257,10 +258,10 @@ public class JacksonAutoConfiguration { private void configurePropertyNamingStrategyField(Jackson2ObjectMapperBuilder builder, String fieldName) { // Find the field (this way we automatically support new constants // that may be added by Jackson in the future) - Field field = ReflectionUtils.findField(PropertyNamingStrategy.class, fieldName, + Field field = ReflectionUtils.findField(PropertyNamingStrategies.class, fieldName, PropertyNamingStrategy.class); Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found on " - + PropertyNamingStrategy.class.getName()); + + PropertyNamingStrategies.class.getName()); try { builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null)); } 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 7ec582a81e..5c6a1227d9 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 @@ -50,7 +50,7 @@ public class JacksonProperties { private String dateFormat; /** - * One of the constants on Jackson's PropertyNamingStrategy. Can also be a + * One of the constants on Jackson's PropertyNamingStrategies. Can also be a * fully-qualified class name of a PropertyNamingStrategy subclass. */ private String propertyNamingStrategy; 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 afcdcfe1c7..b19a65c376 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 @@ -37,7 +37,7 @@ import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy; +import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -132,7 +132,7 @@ class JacksonAutoConfigurationTests { @Test void customPropertyNamingStrategyClass() { this.contextRunner.withPropertyValues( - "spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy") + "spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy") .run((context) -> { ObjectMapper mapper = context.getBean(ObjectMapper.class); assertThat(mapper.getPropertyNamingStrategy()).isInstanceOf(SnakeCaseStrategy.class); diff --git a/spring-boot-project/spring-boot-dependencies/build.gradle b/spring-boot-project/spring-boot-dependencies/build.gradle index 648a1350be..2523476c2a 100644 --- a/spring-boot-project/spring-boot-dependencies/build.gradle +++ b/spring-boot-project/spring-boot-dependencies/build.gradle @@ -493,7 +493,7 @@ bom { ] } } - library("Jackson Bom", "2.11.3") { + library("Jackson Bom", "2.12.0") { group("com.fasterxml.jackson") { imports = [ "jackson-bom" diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapper.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapper.java index b73ac09dda..6b71e53d45 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapper.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapper.java @@ -18,7 +18,7 @@ package org.springframework.boot.buildpack.platform.json; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; @@ -37,7 +37,7 @@ public final class SharedObjectMapper { objectMapper.registerModule(new ParameterNamesModule()); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE); + objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE); INSTANCE = objectMapper; } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java index c7cb7524d8..6c180c2bfc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.buildpack.platform.json; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; @@ -42,9 +43,9 @@ class SharedObjectMapperTests { assertThat(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES .enabledIn(mapper.getDeserializationConfig().getDeserializationFeatures())).isFalse(); assertThat(mapper.getSerializationConfig().getPropertyNamingStrategy()) - .isEqualTo(PropertyNamingStrategy.LOWER_CAMEL_CASE); + .isEqualTo(PropertyNamingStrategies.LOWER_CAMEL_CASE); assertThat(mapper.getDeserializationConfig().getPropertyNamingStrategy()) - .isEqualTo(PropertyNamingStrategy.LOWER_CAMEL_CASE); + .isEqualTo(PropertyNamingStrategies.LOWER_CAMEL_CASE); } } From a6c6655c8228553f7e4e9bf0b9817bcc83ef3927 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 21 Dec 2020 15:49:23 +0100 Subject: [PATCH 2/2] Polish "Upgrade to Jackson 2.12.0" Guard breaking change to PropertyNamingStrategies so that we tolerate older Jackson versions. See gh-24415 --- .../jackson/JacksonAutoConfiguration.java | 18 ++++-- .../jackson/JacksonProperties.java | 2 +- .../Jackson211AutoConfigurationTests.java | 57 +++++++++++++++++++ .../JacksonAutoConfigurationTests.java | 2 +- .../json/SharedObjectMapperTests.java | 1 - 5 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/Jackson211AutoConfigurationTests.java 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 8b7327a308..c806e09881 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 @@ -32,7 +32,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; @@ -258,10 +257,8 @@ public class JacksonAutoConfiguration { private void configurePropertyNamingStrategyField(Jackson2ObjectMapperBuilder builder, String fieldName) { // Find the field (this way we automatically support new constants // that may be added by Jackson in the future) - Field field = ReflectionUtils.findField(PropertyNamingStrategies.class, fieldName, - PropertyNamingStrategy.class); - Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found on " - + PropertyNamingStrategies.class.getName()); + Field field = findPropertyNamingStrategyField(fieldName); + Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found"); try { builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null)); } @@ -270,6 +267,17 @@ public class JacksonAutoConfiguration { } } + private Field findPropertyNamingStrategyField(String fieldName) { + try { + return ReflectionUtils.findField(com.fasterxml.jackson.databind.PropertyNamingStrategies.class, + fieldName, PropertyNamingStrategy.class); + } + catch (NoClassDefFoundError ex) { // Fallback pre Jackson 2.12 + return ReflectionUtils.findField(PropertyNamingStrategy.class, fieldName, + PropertyNamingStrategy.class); + } + } + private void configureModules(Jackson2ObjectMapperBuilder builder) { Collection moduleBeans = getBeans(this.applicationContext, Module.class); builder.modulesToInstall(moduleBeans.toArray(new Module[0])); 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 5c6a1227d9..f11baa6f48 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 @@ -51,7 +51,7 @@ public class JacksonProperties { /** * One of the constants on Jackson's PropertyNamingStrategies. Can also be a - * fully-qualified class name of a PropertyNamingStrategy subclass. + * fully-qualified class name of a PropertyNamingStrategy implementation. */ private String propertyNamingStrategy; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/Jackson211AutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/Jackson211AutoConfigurationTests.java new file mode 100644 index 0000000000..7e6a9fd9dc --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/Jackson211AutoConfigurationTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.jackson; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; +import org.springframework.boot.testsupport.classpath.ClassPathOverrides; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link JacksonAutoConfiguration} using Jackson 2.11.x + * + * @author Stephane Nicoll + */ +@ClassPathExclusions({ "jackson-databind*.jar", "jackson-dataformat-xml*.jar" }) +@ClassPathOverrides({ "com.fasterxml.jackson.core:jackson-databind:2.11.3", + "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.3" }) +public class Jackson211AutoConfigurationTests extends JacksonAutoConfigurationTests { + + public static final String STRATEGY_CLASS_NAME = "com.fasterxml.jackson.databind.PropertyNamingStrategy$SnakeCaseStrategy"; + + @Test + void customPropertyNamingStrategyField() { + this.contextRunner.withPropertyValues("spring.jackson.property-naming-strategy:SNAKE_CASE").run((context) -> { + ObjectMapper mapper = context.getBean(ObjectMapper.class); + assertThat(mapper.getPropertyNamingStrategy().getClass().getName()).isEqualTo(STRATEGY_CLASS_NAME); + }); + } + + @Test + void customPropertyNamingStrategyClass() { + this.contextRunner.withPropertyValues( + "spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy") + .run((context) -> { + ObjectMapper mapper = context.getBean(ObjectMapper.class); + assertThat(mapper.getPropertyNamingStrategy().getClass().getName()).isEqualTo(STRATEGY_CLASS_NAME); + }); + } + +} 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 b19a65c376..6c770b0298 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 @@ -72,7 +72,7 @@ import static org.mockito.Mockito.mock; */ class JacksonAutoConfigurationTests { - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + protected final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class)); @Test diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java index 6c180c2bfc..16a7b2844d 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java @@ -19,7 +19,6 @@ package org.springframework.boot.buildpack.platform.json; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import org.junit.jupiter.api.Test;