Skip to content

Commit 71648d7

Browse files
committed
migrate from jackson 2 to 3
Signed-off-by: kvmw <mshamsi@broadcom.com>
1 parent 3201854 commit 71648d7

File tree

10 files changed

+54
-64
lines changed

10 files changed

+54
-64
lines changed

spring-credhub-core/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies {
2222
api("org.springframework:spring-beans")
2323
api("org.springframework:spring-context")
2424
api("org.springframework:spring-web")
25-
api("com.fasterxml.jackson.core:jackson-databind")
25+
api("tools.jackson.core:jackson-databind")
2626
api("org.springframework.security:spring-security-oauth2-client") {
2727
exclude module: "spring-security-web"
2828
}
@@ -37,6 +37,7 @@ dependencies {
3737
}
3838

3939
testImplementation("org.springframework:spring-test")
40+
testImplementation("org.springframework.boot:spring-boot-jackson2")
4041
testImplementation("io.projectreactor:reactor-test")
4142
testImplementation("org.junit.jupiter:junit-jupiter-api")
4243
testImplementation("org.junit.jupiter:junit-jupiter-params")

spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubRestTemplateFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
3333
import org.springframework.http.converter.FormHttpMessageConverter;
3434
import org.springframework.http.converter.StringHttpMessageConverter;
35-
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
35+
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
3636
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
3737
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
3838
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
@@ -132,9 +132,8 @@ private static void configureRestTemplate(RestTemplate restTemplate, String base
132132
restTemplate.setRequestFactory(clientHttpRequestFactory);
133133
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(baseUri));
134134
restTemplate.getInterceptors().add(new CredHubRequestInterceptor());
135-
restTemplate
136-
.setMessageConverters(Arrays.asList(new ByteArrayHttpMessageConverter(), new StringHttpMessageConverter(),
137-
new MappingJackson2HttpMessageConverter(JsonUtils.buildObjectMapper())));
135+
restTemplate.setMessageConverters(Arrays.asList(new ByteArrayHttpMessageConverter(),
136+
new StringHttpMessageConverter(), new JacksonJsonHttpMessageConverter(JsonUtils.buildJsonMapper())));
138137
}
139138

140139
/**

spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubWebClientFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package org.springframework.credhub.core;
1818

19-
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import tools.jackson.databind.json.JsonMapper;
2020

2121
import org.springframework.credhub.support.utils.JsonUtils;
2222
import org.springframework.http.HttpHeaders;
2323
import org.springframework.http.MediaType;
2424
import org.springframework.http.client.reactive.ClientHttpConnector;
2525
import org.springframework.http.codec.CodecConfigurer;
26-
import org.springframework.http.codec.json.Jackson2JsonDecoder;
27-
import org.springframework.http.codec.json.Jackson2JsonEncoder;
26+
import org.springframework.http.codec.json.JacksonJsonDecoder;
27+
import org.springframework.http.codec.json.JacksonJsonEncoder;
2828
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager;
2929
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProvider;
3030
import org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProviderBuilder;
@@ -126,11 +126,11 @@ private static DefaultReactiveOAuth2AuthorizedClientManager buildClientManager(
126126

127127
private static WebClient.Builder buildWebClient(String baseUri, ClientHttpConnector clientHttpConnector) {
128128
ExchangeStrategies strategies = ExchangeStrategies.builder().codecs((configurer) -> {
129-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
129+
JsonMapper mapper = JsonUtils.buildJsonMapper();
130130

131131
CodecConfigurer.DefaultCodecs dc = configurer.defaultCodecs();
132-
dc.jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
133-
dc.jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
132+
dc.jacksonJsonDecoder(new JacksonJsonDecoder(mapper));
133+
dc.jacksonJsonEncoder(new JacksonJsonEncoder(mapper));
134134
}).build();
135135

136136
return WebClient.builder()

spring-credhub-core/src/main/java/org/springframework/credhub/support/utils/JsonUtils.java

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
package org.springframework.credhub.support.utils;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.Arrays;
2120

2221
import com.fasterxml.jackson.annotation.JsonInclude;
23-
import com.fasterxml.jackson.databind.DeserializationFeature;
24-
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
26-
import com.fasterxml.jackson.databind.SerializationFeature;
27-
import com.fasterxml.jackson.databind.jsontype.NamedType;
28-
import com.fasterxml.jackson.databind.util.StdDateFormat;
22+
import tools.jackson.databind.DeserializationFeature;
23+
import tools.jackson.databind.MapperFeature;
24+
import tools.jackson.databind.PropertyNamingStrategies;
25+
import tools.jackson.databind.cfg.EnumFeature;
26+
import tools.jackson.databind.json.JsonMapper;
27+
import tools.jackson.databind.jsontype.NamedType;
28+
import tools.jackson.databind.util.StdDateFormat;
2929

3030
import org.springframework.credhub.support.CredentialType;
3131

@@ -40,40 +40,32 @@ private JsonUtils() {
4040
}
4141

4242
/**
43-
* Create and configure the {@link ObjectMapper} used for serializing and
44-
* deserializing JSON requests and responses.
45-
* @return a configured {@link ObjectMapper}
43+
* Create and configure the {@link JsonMapper} used for serializing and deserializing
44+
* JSON requests and responses.
45+
* @return a configured {@link JsonMapper}
4646
*/
47-
public static ObjectMapper buildObjectMapper() {
48-
ObjectMapper objectMapper = new ObjectMapper();
49-
objectMapper.setDateFormat(new StdDateFormat());
50-
objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy());
51-
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
52-
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
53-
objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
54-
objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
55-
56-
configureCredentialDetailTypeMapping(objectMapper);
57-
58-
return objectMapper;
47+
public static JsonMapper buildJsonMapper() {
48+
return JsonMapper.builder()
49+
.defaultDateFormat(new StdDateFormat())
50+
.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE)
51+
.changeDefaultPropertyInclusion((incl) -> incl.withValueInclusion(JsonInclude.Include.NON_EMPTY))
52+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
53+
.configure(EnumFeature.READ_ENUMS_USING_TO_STRING, true)
54+
.configure(EnumFeature.WRITE_ENUMS_USING_TO_STRING, true)
55+
.configure(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS, true)
56+
.registerSubtypes(credentialDetailTypeMappings())
57+
.build();
5958
}
6059

6160
/**
62-
* Configure type mapping for the {@literal value} field in the
63-
* {@literal CredentialDetails} object.
64-
* @param objectMapper the {@link ObjectMapper} to configure
61+
* Return type mappings for the {@literal value} field in the
62+
* {@literal CredentialDetails} objects.
63+
* @return array of {@link NamedType} for {@literal CredentialDetails}.
6564
*/
66-
private static void configureCredentialDetailTypeMapping(ObjectMapper objectMapper) {
67-
List<NamedType> subtypes = new ArrayList<>();
68-
for (CredentialType type : CredentialType.values()) {
69-
subtypes.add(new NamedType(type.getModelClass(), type.getValueType()));
70-
}
71-
72-
registerSubtypes(objectMapper, subtypes);
73-
}
74-
75-
private static void registerSubtypes(ObjectMapper objectMapper, List<NamedType> subtypes) {
76-
objectMapper.registerSubtypes(subtypes.toArray(new NamedType[] {}));
65+
private static NamedType[] credentialDetailTypeMappings() {
66+
return Arrays.stream(CredentialType.values())
67+
.map((type) -> new NamedType(type.getModelClass(), type.getValueType()))
68+
.toArray(NamedType[]::new);
7769
}
7870

7971
}

spring-credhub-core/src/test/java/org/springframework/credhub/core/interpolation/CredHubInterpolationTemplateUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818

1919
import java.io.IOException;
2020

21-
import com.fasterxml.jackson.databind.ObjectMapper;
2221
import org.junit.jupiter.api.BeforeEach;
2322
import org.junit.jupiter.api.Test;
2423
import org.junit.jupiter.api.extension.ExtendWith;
2524
import org.mockito.Mock;
2625
import org.mockito.junit.jupiter.MockitoExtension;
2726
import org.mockito.junit.jupiter.MockitoSettings;
2827
import org.mockito.quality.Strictness;
28+
import tools.jackson.databind.json.JsonMapper;
2929

3030
import org.springframework.credhub.core.CredHubTemplate;
3131
import org.springframework.credhub.support.ServiceInstanceCredentialName;
@@ -92,7 +92,7 @@ private ServicesData buildVcapServices(String credHubReferenceName) throws IOExc
9292
}
9393
""".formatted(credHubReferenceName);
9494

95-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
95+
JsonMapper mapper = JsonUtils.buildJsonMapper();
9696
return mapper.readValue(vcapServices, ServicesData.class);
9797
}
9898

spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.util.Date;
2020

21-
import com.fasterxml.jackson.databind.util.StdDateFormat;
2221
import org.junit.jupiter.api.BeforeEach;
22+
import tools.jackson.databind.util.StdDateFormat;
2323

2424
import static org.assertj.core.api.Assertions.assertThat;
2525

spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonTestUtils.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@
1616

1717
package org.springframework.credhub.support;
1818

19-
import java.io.IOException;
20-
21-
import com.fasterxml.jackson.core.JsonProcessingException;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
2319
import com.jayway.jsonpath.Configuration;
2420
import com.jayway.jsonpath.DocumentContext;
2521
import com.jayway.jsonpath.JsonPath;
2622
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
2723
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
24+
import tools.jackson.core.JacksonException;
25+
import tools.jackson.core.exc.JacksonIOException;
26+
import tools.jackson.databind.json.JsonMapper;
2827

2928
import org.springframework.credhub.support.utils.JsonUtils;
3029

@@ -37,21 +36,21 @@ private JsonTestUtils() {
3736

3837
public static String toJson(Object object) {
3938
try {
40-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
39+
JsonMapper mapper = JsonUtils.buildJsonMapper();
4140
return mapper.writeValueAsString(object);
4241
}
43-
catch (JsonProcessingException ex) {
42+
catch (JacksonException ex) {
4443
fail("Error creating JSON string from object: " + ex);
4544
throw new IllegalStateException(ex);
4645
}
4746
}
4847

4948
public static <T> T fromJson(String json, Class<T> type) {
5049
try {
51-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
50+
JsonMapper mapper = JsonUtils.buildJsonMapper();
5251
return mapper.readValue(json, type);
5352
}
54-
catch (IOException ex) {
53+
catch (JacksonIOException ex) {
5554
fail("Error parsing JSON string to object: " + ex);
5655
throw new IllegalStateException(ex);
5756
}

spring-credhub-demo/src/main/java/org/springframework/credhub/demo/CredHubDemoController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424
import java.util.UUID;
2525

26-
import com.fasterxml.jackson.databind.ObjectMapper;
2726
import org.springframework.credhub.core.CredHubOperations;
2827
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
2928
import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations;

spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/InterpolationIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23-
import com.fasterxml.jackson.databind.ObjectMapper;
2423
import org.junit.jupiter.api.AfterEach;
2524
import org.junit.jupiter.api.BeforeEach;
2625
import org.junit.jupiter.api.Test;
26+
import tools.jackson.databind.json.JsonMapper;
2727

2828
import org.springframework.credhub.core.credential.CredHubCredentialOperations;
2929
import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations;
@@ -105,7 +105,7 @@ private ServicesData buildVcapServices(String credHubReferenceName) throws IOExc
105105
}
106106
""".formatted(credHubReferenceName);
107107

108-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
108+
JsonMapper mapper = JsonUtils.buildJsonMapper();
109109
return mapper.readValue(vcapServices, ServicesData.class);
110110
}
111111

spring-credhub-reactive-integration-tests/src/test/java/org/springframework/credhub/integration/ReactiveInterpolationIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23-
import com.fasterxml.jackson.databind.ObjectMapper;
2423
import org.junit.jupiter.api.AfterEach;
2524
import org.junit.jupiter.api.BeforeEach;
2625
import org.junit.jupiter.api.Test;
2726
import reactor.test.StepVerifier;
27+
import tools.jackson.databind.json.JsonMapper;
2828

2929
import org.springframework.credhub.core.credential.ReactiveCredHubCredentialOperations;
3030
import org.springframework.credhub.core.interpolation.ReactiveCredHubInterpolationOperations;
@@ -109,7 +109,7 @@ private ServicesData buildVcapServices(String credHubReferenceName) throws IOExc
109109
}
110110
""".formatted(credHubReferenceName);
111111

112-
ObjectMapper mapper = JsonUtils.buildObjectMapper();
112+
JsonMapper mapper = JsonUtils.buildJsonMapper();
113113
return mapper.readValue(vcapServices, ServicesData.class);
114114
}
115115

0 commit comments

Comments
 (0)