Skip to content

Commit a6c6655

Browse files
committed
Polish "Upgrade to Jackson 2.12.0"
Guard breaking change to PropertyNamingStrategies so that we tolerate older Jackson versions. See gh-24415
1 parent 1f63b82 commit a6c6655

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.fasterxml.jackson.annotation.PropertyAccessor;
3333
import com.fasterxml.jackson.databind.Module;
3434
import com.fasterxml.jackson.databind.ObjectMapper;
35-
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
3635
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
3736
import com.fasterxml.jackson.databind.SerializationFeature;
3837
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
@@ -258,10 +257,8 @@ private void configurePropertyNamingStrategyClass(Jackson2ObjectMapperBuilder bu
258257
private void configurePropertyNamingStrategyField(Jackson2ObjectMapperBuilder builder, String fieldName) {
259258
// Find the field (this way we automatically support new constants
260259
// that may be added by Jackson in the future)
261-
Field field = ReflectionUtils.findField(PropertyNamingStrategies.class, fieldName,
262-
PropertyNamingStrategy.class);
263-
Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found on "
264-
+ PropertyNamingStrategies.class.getName());
260+
Field field = findPropertyNamingStrategyField(fieldName);
261+
Assert.notNull(field, () -> "Constant named '" + fieldName + "' not found");
265262
try {
266263
builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null));
267264
}
@@ -270,6 +267,17 @@ private void configurePropertyNamingStrategyField(Jackson2ObjectMapperBuilder bu
270267
}
271268
}
272269

270+
private Field findPropertyNamingStrategyField(String fieldName) {
271+
try {
272+
return ReflectionUtils.findField(com.fasterxml.jackson.databind.PropertyNamingStrategies.class,
273+
fieldName, PropertyNamingStrategy.class);
274+
}
275+
catch (NoClassDefFoundError ex) { // Fallback pre Jackson 2.12
276+
return ReflectionUtils.findField(PropertyNamingStrategy.class, fieldName,
277+
PropertyNamingStrategy.class);
278+
}
279+
}
280+
273281
private void configureModules(Jackson2ObjectMapperBuilder builder) {
274282
Collection<Module> moduleBeans = getBeans(this.applicationContext, Module.class);
275283
builder.modulesToInstall(moduleBeans.toArray(new Module[0]));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class JacksonProperties {
5151

5252
/**
5353
* One of the constants on Jackson's PropertyNamingStrategies. Can also be a
54-
* fully-qualified class name of a PropertyNamingStrategy subclass.
54+
* fully-qualified class name of a PropertyNamingStrategy implementation.
5555
*/
5656
private String propertyNamingStrategy;
5757

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jackson;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
23+
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link JacksonAutoConfiguration} using Jackson 2.11.x
29+
*
30+
* @author Stephane Nicoll
31+
*/
32+
@ClassPathExclusions({ "jackson-databind*.jar", "jackson-dataformat-xml*.jar" })
33+
@ClassPathOverrides({ "com.fasterxml.jackson.core:jackson-databind:2.11.3",
34+
"com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.3" })
35+
public class Jackson211AutoConfigurationTests extends JacksonAutoConfigurationTests {
36+
37+
public static final String STRATEGY_CLASS_NAME = "com.fasterxml.jackson.databind.PropertyNamingStrategy$SnakeCaseStrategy";
38+
39+
@Test
40+
void customPropertyNamingStrategyField() {
41+
this.contextRunner.withPropertyValues("spring.jackson.property-naming-strategy:SNAKE_CASE").run((context) -> {
42+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
43+
assertThat(mapper.getPropertyNamingStrategy().getClass().getName()).isEqualTo(STRATEGY_CLASS_NAME);
44+
});
45+
}
46+
47+
@Test
48+
void customPropertyNamingStrategyClass() {
49+
this.contextRunner.withPropertyValues(
50+
"spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy")
51+
.run((context) -> {
52+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
53+
assertThat(mapper.getPropertyNamingStrategy().getClass().getName()).isEqualTo(STRATEGY_CLASS_NAME);
54+
});
55+
}
56+
57+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
*/
7373
class JacksonAutoConfigurationTests {
7474

75-
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
75+
protected final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
7676
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class));
7777

7878
@Test

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/json/SharedObjectMapperTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.fasterxml.jackson.databind.DeserializationFeature;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
22-
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
2322
import com.fasterxml.jackson.databind.SerializationFeature;
2423
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
2524
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)