From 06d4003f6609bb255f7b0ff13c6850e3fc32ba97 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Fri, 19 May 2017 14:01:03 -0500 Subject: [PATCH] Centralize jackson configuration. --- .../credhub/core/CredHubClient.java | 8 +-- .../credhub/core/JsonUtils.java | 65 +++++++++++++++++++ .../credhub/support/CredentialDetails.java | 11 ---- .../support/CredentialDetailsData.java | 7 -- .../credhub/support/CredentialSummary.java | 6 -- .../support/CredentialSummaryData.java | 6 -- .../credhub/support/JsonWriteRequest.java | 4 -- .../credhub/support/PasswordWriteRequest.java | 4 -- .../credhub/support/WriteRequest.java | 6 -- .../core/CredHubTemplateUnitTests.java | 2 +- .../support/JsonParsingUnitTestsBase.java | 9 +-- .../support/WriteRequestUnitTestsBase.java | 3 +- 12 files changed, 72 insertions(+), 59 deletions(-) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java index 5ceed36..011bf8a 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubClient.java @@ -21,9 +21,6 @@ package org.springframework.credhub.core; import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.util.ISO8601DateFormat; - import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.converter.ByteArrayHttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter; @@ -77,13 +74,10 @@ public class CredHubClient { * @return the list of {@link HttpMessageConverter}s */ private static List> createMessageConverters() { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.setDateFormat(new ISO8601DateFormat()); - List> messageConverters = new ArrayList>(3); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter()); - messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper)); + messageConverters.add(new MappingJackson2HttpMessageConverter(JsonUtils.buildObjectMapper())); return messageConverters; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java new file mode 100644 index 0000000..4aaed81 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016-2017 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 + * + * http://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.credhub.core; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.jsontype.NamedType; +import com.fasterxml.jackson.databind.util.ISO8601DateFormat; + +import org.springframework.credhub.support.JsonCredential; +import org.springframework.credhub.support.ValueType; + +/** + * Utility methods for configuring JSON serialization and deserialization. + * + * @author Scott Frederick + */ +public class JsonUtils { + /** + * Create and configure the {@link ObjectMapper} used for serializing and deserializing + * JSON requests and responses. + * + * @return a configured {@link ObjectMapper} + */ + public static ObjectMapper buildObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setDateFormat(new ISO8601DateFormat()); + objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy()); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + configureCredentialDetailValueTypeMapping(objectMapper); + + return objectMapper; + } + + /** + * Configure type mapping for the {@literal value} field in the {@literal CredentialDetails} + * object. + * + * @param objectMapper the {@link ObjectMapper} to configure + */ + private static void configureCredentialDetailValueTypeMapping(ObjectMapper objectMapper) { + objectMapper.registerSubtypes( + new NamedType(String.class, ValueType.PASSWORD.type()), + new NamedType(JsonCredential.class, ValueType.JSON.type()) + ); + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java index 830653a..ef0f3c5 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java @@ -16,13 +16,8 @@ package org.springframework.credhub.support; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonTypeInfo; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; /** * The details of a credential that has been written to CredHub. Clients don't @@ -32,8 +27,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; * * @author Scott Frederick */ -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class CredentialDetails extends CredentialSummary { private String id; @@ -41,10 +34,6 @@ public class CredentialDetails extends CredentialSummary { private ValueType valueType; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type") - @JsonSubTypes({ - @Type(value = String.class, name = "password"), - @Type(value = JsonCredential.class, name = "json") - }) private T value; /** diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java index 30c59e2..fa6c459 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java @@ -21,11 +21,6 @@ package org.springframework.credhub.support; import java.util.Arrays; import java.util.List; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.annotation.JsonInclude; - -import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; - /** * A collection of {@link CredentialDetails}. Clients don't typically instantiate * objects of this type, but will receive them in response to write and retrieve @@ -33,8 +28,6 @@ import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; * * @author Scott Frederick */ -@JsonInclude(NON_EMPTY) -@JsonIgnoreProperties(ignoreUnknown = true) public class CredentialDetailsData { private List> data; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java index b6014e8..f21e2d1 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java @@ -18,10 +18,6 @@ package org.springframework.credhub.support; import java.util.Date; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; - /** * A summary of a credential that has been written to CredHub. Clients don't typically * instantiate objects of this type, but will receive them in response to write and @@ -29,8 +25,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; * * @author Scott Frederick */ -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class CredentialSummary { protected CredentialName name; protected Date versionCreatedAt; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java index 4b3631d..e88f46c 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java @@ -19,10 +19,6 @@ package org.springframework.credhub.support; import java.util.Arrays; import java.util.List; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; - /** * A collection of {@link CredentialSummary}s. Clients don't typically instantiate * objects of this type, but will receive them in response to write and retrieve @@ -30,8 +26,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; * * @author Scott Frederick */ -@JsonIgnoreProperties(ignoreUnknown = true) -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class CredentialSummaryData { private List credentials; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java index b1d5b64..d2a08a4 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java @@ -16,9 +16,6 @@ package org.springframework.credhub.support; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; - import org.springframework.util.Assert; import java.util.Map; @@ -30,7 +27,6 @@ import static org.springframework.credhub.support.ValueType.JSON; * * @author Scott Frederick */ -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class JsonWriteRequest extends WriteRequest { /** * Create a builder that provides a fluent API for providing the values required diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java index f1ad397..dec3a34 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java @@ -16,9 +16,6 @@ package org.springframework.credhub.support; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; - import org.springframework.util.Assert; import static org.springframework.credhub.support.ValueType.PASSWORD; @@ -28,7 +25,6 @@ import static org.springframework.credhub.support.ValueType.PASSWORD; * * @author Scott Frederick */ -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class PasswordWriteRequest extends WriteRequest { /** * Create a builder that provides a fluent API for providing the values required diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java index dcc44c7..6a4b188 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java @@ -24,25 +24,19 @@ import java.util.Collection; import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.annotation.JsonNaming; import org.springframework.util.Assert; -import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; - /** * The details of a request to write a new or update an existing credential in CredHub. * * @author Scott Frederick */ -@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) public class WriteRequest { private boolean overwrite; private CredentialName name; private ValueType valueType; private T value; - @JsonInclude(NON_EMPTY) private List additionalPermissions; /** diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTests.java index 2cfdbf3..576250d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateUnitTests.java @@ -94,7 +94,7 @@ public class CredHubTemplateUnitTests extends CredHubTemplateUnitTestsBase { " ]" + "}"; - ObjectMapper mapper = new ObjectMapper(); + ObjectMapper mapper = JsonUtils.buildObjectMapper(); return mapper.readValue(vcapServices, VcapServicesData.class); } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java index 1d984fe..78030a6 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java @@ -16,12 +16,12 @@ package org.springframework.credhub.support; -import java.text.DateFormat; import java.util.Date; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.util.ISO8601DateFormat; import org.junit.Before; +import org.springframework.credhub.core.JsonUtils; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; @@ -47,12 +47,9 @@ public abstract class JsonParsingUnitTestsBase { @Before public void setUpJsonParsing() throws Exception { - DateFormat dateFormat = new ISO8601DateFormat(); + objectMapper = JsonUtils.buildObjectMapper(); - objectMapper = new ObjectMapper(); - objectMapper.setDateFormat(dateFormat); - - testDate = dateFormat.parse(TEST_DATE_STRING); + testDate = new ISO8601DateFormat().parse(TEST_DATE_STRING); } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java index 08e3631..ec02290 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Before; import org.junit.Test; +import org.springframework.credhub.core.JsonUtils; import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder; import static org.hamcrest.CoreMatchers.allOf; @@ -37,7 +38,7 @@ public abstract class WriteRequestUnitTestsBase { @Before public void setUpWriteRequestUnitTestsBase() { - mapper = new ObjectMapper(); + mapper = JsonUtils.buildObjectMapper(); } @Test