From af0305b43ccc5ade7884f232bb1fae8114653582 Mon Sep 17 00:00:00 2001 From: Sashir Estela Date: Thu, 23 Jan 2025 03:51:06 +0000 Subject: [PATCH] Improve dependency between JsonUtil and Configurator --- .../cleverclient/support/Configurator.java | 2 ++ .../cleverclient/util/JsonUtil.java | 28 +++++++++++-------- .../cleverclient/CleverClientTest.java | 7 ----- .../cleverclient/http/HttpProcessorTest.java | 7 ----- 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java index a729a23..6abfc81 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java +++ b/src/main/java/io/github/sashirestela/cleverclient/support/Configurator.java @@ -1,6 +1,7 @@ package io.github.sashirestela.cleverclient.support; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.sashirestela.cleverclient.util.JsonUtil; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -29,6 +30,7 @@ public Configurator(@Singular("endOfStream") List endsOfStream, ObjectMa } configurator.endsOfStream = endsOfStream; configurator.objectMapper = objectMapper; + JsonUtil.updateObjectMapper(objectMapper); configurator.wasBuilt = true; } diff --git a/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java b/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java index 662c896..669fff6 100644 --- a/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java +++ b/src/main/java/io/github/sashirestela/cleverclient/util/JsonUtil.java @@ -8,7 +8,6 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.type.CollectionType; import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.support.Configurator; import java.io.IOException; import java.util.ArrayList; @@ -17,17 +16,24 @@ public class JsonUtil { - private static final ObjectMapper objectMapperStrict = Configurator.one().getObjectMapper(); - - private static final ObjectReader objectReaderIgnoringUnknown = objectMapperStrict.reader() + private static ObjectMapper objectMapper = new ObjectMapper(); + private static ObjectReader objectReader = objectMapper.reader() .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); private JsonUtil() { } + public static void updateObjectMapper(ObjectMapper newObjectMapper) { + synchronized (JsonUtil.class) { + objectMapper = newObjectMapper != null ? newObjectMapper : new ObjectMapper(); + objectReader = objectMapper.reader() + .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + } + } + public static Map objectToMap(T object) { try { - return objectMapperStrict.convertValue(object, new TypeReference<>() { + return objectMapper.convertValue(object, new TypeReference<>() { }); } catch (IllegalArgumentException e) { throw new CleverClientException("Cannot convert object {0} to Map.", object, e); @@ -36,7 +42,7 @@ public static Map objectToMap(T object) { public static String objectToJson(T object) { try { - return objectMapperStrict.writeValueAsString(object); + return objectMapper.writeValueAsString(object); } catch (JsonProcessingException e) { throw new CleverClientException("Cannot convert the object {0} to Json.", object, e); } @@ -44,7 +50,7 @@ public static String objectToJson(T object) { public static T jsonToObject(String json, Class clazz) { try { - return objectReaderIgnoringUnknown.readValue(json, clazz); + return objectReader.readValue(json, clazz); } catch (IOException e) { throw new CleverClientException("Cannot convert the Json {0} to class {1}.", json, clazz.getName(), e); } @@ -52,9 +58,9 @@ public static T jsonToObject(String json, Class clazz) { public static List jsonToList(String json, Class clazz) { try { - CollectionType listType = objectReaderIgnoringUnknown.getTypeFactory() + CollectionType listType = objectReader.getTypeFactory() .constructCollectionType(ArrayList.class, clazz); - return objectReaderIgnoringUnknown.forType(listType).readValue(json); + return objectReader.forType(listType).readValue(json); } catch (JsonProcessingException e) { throw new CleverClientException("Cannot convert the Json {0} to List of {1}.", json, clazz.getName(), e); } @@ -62,9 +68,9 @@ public static List jsonToList(String json, Class clazz) { public static T jsonToParametricObject(String json, Class clazzT, Class clazzU) { try { - JavaType javaType = objectReaderIgnoringUnknown.getTypeFactory() + JavaType javaType = objectReader.getTypeFactory() .constructParametricType(clazzT, clazzU); - return objectReaderIgnoringUnknown.forType(javaType).readValue(json); + return objectReader.forType(javaType).readValue(json); } catch (JsonProcessingException e) { throw new CleverClientException("Cannot convert the Json {0} to class of {1}.", json, clazzT.getName(), e); } diff --git a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java index 09fc945..f2b2129 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/CleverClientTest.java @@ -7,11 +7,9 @@ import io.github.sashirestela.cleverclient.client.JavaHttpClientAdapter; import io.github.sashirestela.cleverclient.http.HttpRequestData; import io.github.sashirestela.cleverclient.support.ContentType; -import io.github.sashirestela.cleverclient.test.TestSupport; import io.github.sashirestela.cleverclient.util.HttpRequestBodyTestUtility; import lombok.Builder; import lombok.Value; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -36,11 +34,6 @@ class CleverClientTest { - @BeforeAll - static void setup() { - TestSupport.setupConfigurator(); - } - @Test void shouldSetPropertiesToDefaultValuesWhenBuilderIsCalledWithoutThoseProperties() { var cleverClient = CleverClient.builder() diff --git a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java index 59a3ce1..606295b 100644 --- a/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java +++ b/src/test/java/io/github/sashirestela/cleverclient/http/HttpProcessorTest.java @@ -1,9 +1,7 @@ package io.github.sashirestela.cleverclient.http; import io.github.sashirestela.cleverclient.support.CleverClientException; -import io.github.sashirestela.cleverclient.test.TestSupport; import io.github.sashirestela.cleverclient.test.TestSupport.SyncType; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; @@ -53,11 +51,6 @@ interface HttpProcessorTest { void testShutdown(); - @BeforeAll - static void setup() { - TestSupport.setupConfigurator(); - } - @Test default void shouldThownExceptionWhenCallingMethodReturnTypeIsUnsupported() { var service = getHttpProcessor().createProxy(ITest.AsyncService.class);