diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/MappingDocumentDbConverter.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/MappingDocumentDbConverter.java index b5b744f..f16d8f7 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/MappingDocumentDbConverter.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/MappingDocumentDbConverter.java @@ -53,8 +53,6 @@ public class MappingDocumentDbConverter this.conversionService = new GenericConversionService(); this.objectMapper = objectMapper == null ? ObjectMapperFactory.getObjectMapper() : objectMapper; - this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - this.objectMapper.registerModule(provideAdvancedSerializersModule()); } @Override @@ -69,6 +67,7 @@ public class MappingDocumentDbConverter return readInternal(entity, type, cosmosItemProperties); } + private R readInternal(final DocumentDbPersistentEntity entity, Class type, final CosmosItemProperties cosmosItemProperties) { @@ -90,12 +89,6 @@ public class MappingDocumentDbConverter } } - private SimpleModule provideAdvancedSerializersModule() { - final SimpleModule simpleModule = new SimpleModule(); - simpleModule.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer()); - return simpleModule; - } - @Override @Deprecated public void write(Object sourceEntity, CosmosItemProperties document) { diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ObjectMapperFactory.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ObjectMapperFactory.java index 5411d2d..c6b526f 100644 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ObjectMapperFactory.java +++ b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ObjectMapperFactory.java @@ -6,6 +6,7 @@ package com.microsoft.azure.spring.data.cosmosdb.core.convert; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; @@ -18,10 +19,12 @@ public class ObjectMapperFactory { OBJECT_MAPPER.registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()); + OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public static ObjectMapper getObjectMapper() { return OBJECT_MAPPER; } + } diff --git a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializer.java b/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializer.java deleted file mode 100644 index d54c497..0000000 --- a/src/main/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializer.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. See LICENSE in the project root for - * license information. - */ -package com.microsoft.azure.spring.data.cosmosdb.core.convert; - -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; - -import java.io.IOException; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeParseException; - -import static com.microsoft.azure.spring.data.cosmosdb.Constants.ISO_8601_COMPATIBLE_DATE_PATTERN; - -public class ZonedDateTimeDeserializer extends JsonDeserializer { - - @Override - public ZonedDateTime deserialize(final JsonParser jsonParser, DeserializationContext deserializationContext) - throws IOException { - return parse(jsonParser); - } - - public ZonedDateTime parse(final JsonParser jsonParser) throws IOException { - if (jsonParser.getValueAsString() == null) { - return null; - } - - try { - return ZonedDateTime.parse(jsonParser.getValueAsString(), - DateTimeFormatter.ofPattern(ISO_8601_COMPATIBLE_DATE_PATTERN)); - } catch (DateTimeParseException e) { - throw new JsonParseException(jsonParser, jsonParser.getValueAsString(), e); - } - } - -} diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java index efb20db..092914e 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplateIT.java @@ -66,7 +66,6 @@ public class DocumentDbTemplateIT { private DocumentDbTemplate dbTemplate; private MappingDocumentDbConverter dbConverter; private DocumentDbMappingContext mappingContext; - private ObjectMapper objectMapper; private DocumentCollection collectionPerson; private DocumentDbEntityInformation personInfo; private String collectionName; @@ -80,13 +79,12 @@ public class DocumentDbTemplateIT { final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig); mappingContext = new DocumentDbMappingContext(); - objectMapper = new ObjectMapper(); personInfo = new DocumentDbEntityInformation<>(Person.class); collectionName = personInfo.getCollectionName(); mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); - dbConverter = new MappingDocumentDbConverter(mappingContext, objectMapper); + dbConverter = new MappingDocumentDbConverter(mappingContext, null); dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME); collectionPerson = dbTemplate.createCollectionIfNotExists(this.personInfo); diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplatePartitionIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplatePartitionIT.java index 1c3049a..8557179 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplatePartitionIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/DocumentDbTemplatePartitionIT.java @@ -69,13 +69,12 @@ public class DocumentDbTemplatePartitionIT { public void setup() throws ClassNotFoundException { final DocumentDBConfig dbConfig = DocumentDBConfig.builder(documentDbUri, documentDbKey, DB_NAME).build(); final CosmosDbFactory cosmosDbFactory = new CosmosDbFactory(dbConfig); - final ObjectMapper objectMapper = new ObjectMapper(); final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext(); personInfo = new DocumentDbEntityInformation<>(PartitionPerson.class); mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); - final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, objectMapper); + final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, null); dbTemplate = new DocumentDbTemplate(cosmosDbFactory, dbConverter, DB_NAME); collectionName = personInfo.getCollectionName(); diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplateIT.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplateIT.java index cd62c86..cd9f34b 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplateIT.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/ReactiveCosmosTemplateIT.java @@ -81,13 +81,12 @@ public class ReactiveCosmosTemplateIT { final CosmosDbFactory dbFactory = new CosmosDbFactory(dbConfig); final DocumentDbMappingContext mappingContext = new DocumentDbMappingContext(); - final ObjectMapper objectMapper = new ObjectMapper(); final DocumentDbEntityInformation personInfo = new DocumentDbEntityInformation<>(Person.class); containerName = personInfo.getCollectionName(); mappingContext.setInitialEntitySet(new EntityScanner(this.applicationContext).scan(Persistent.class)); - final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, objectMapper); + final MappingDocumentDbConverter dbConverter = new MappingDocumentDbConverter(mappingContext, null); cosmosTemplate = new ReactiveCosmosTemplate(dbFactory, dbConverter, DB_NAME); cosmosTemplate.createCollectionIfNotExists(personInfo).block().container(); diff --git a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializerTest.java b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializerTest.java index 5a80535..35f02b1 100644 --- a/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializerTest.java +++ b/src/test/java/com/microsoft/azure/spring/data/cosmosdb/core/convert/ZonedDateTimeDeserializerTest.java @@ -5,40 +5,47 @@ */ package com.microsoft.azure.spring.data.cosmosdb.core.convert; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonParser; import org.junit.Test; -import org.mockito.Mockito; import java.io.IOException; import java.time.ZoneId; import java.time.ZonedDateTime; +import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME; import static org.assertj.core.api.Java6Assertions.assertThat; -import static org.mockito.Mockito.when; + public class ZonedDateTimeDeserializerTest { - - private static final String EXAMPLE_DATE_STRING = "2018-10-08T15:06:07:992Z"; - private static final String ILLEGAL_DATE_STRING = "illegal-date-string"; - private static final ZonedDateTime EXPECTED_SERIALIZED_ZONED_DATE_TIME - = ZonedDateTime.of(2018, 10, 8, 15, 6, 7, 992000000, ZoneId.of("Z")); + private static final ZonedDateTime ZONED_DATE_TIME + = ZonedDateTime.of(2018, 10, 8, 15, 6, 7, 992000000, ZoneId.of("UTC")); + private static final String OFFSET_DATE_TIME_WRAPPER_JSON = "{ \"zonedDateTime\": \"" + + ZONED_DATE_TIME.format(ISO_OFFSET_DATE_TIME) + "\" }"; + private static final String ZONED_DATE_TIME_WRAPPER_JSON = "{ \"zonedDateTime\": \"" + + ZONED_DATE_TIME.format(ISO_OFFSET_DATE_TIME) + "\" }"; @Test - public void parse() throws IOException { - final JsonParser jsonParser = Mockito.mock(JsonParser.class); - when(jsonParser.getValueAsString()).thenReturn(EXAMPLE_DATE_STRING); - - final ZonedDateTime zonedDateTime = new ZonedDateTimeDeserializer().parse(jsonParser); - - assertThat(zonedDateTime.equals(EXPECTED_SERIALIZED_ZONED_DATE_TIME)).isTrue(); + public void deserializeZonedDateTime() throws IOException { + final ZonedDateTimeWrapper wrapper = ObjectMapperFactory.getObjectMapper() + .readValue(ZONED_DATE_TIME_WRAPPER_JSON, ZonedDateTimeWrapper.class); + assertThat(wrapper.getZonedDateTime()).isEqualTo(ZONED_DATE_TIME); } - @Test(expected = JsonParseException.class) - public void testParseException() throws IOException { - final JsonParser jsonParser = Mockito.mock(JsonParser.class); - when(jsonParser.getValueAsString()).thenReturn(ILLEGAL_DATE_STRING); + @Test + public void deserializeOffsetDateTime() throws IOException { + final ZonedDateTimeWrapper wrapper = ObjectMapperFactory.getObjectMapper() + .readValue(OFFSET_DATE_TIME_WRAPPER_JSON, ZonedDateTimeWrapper.class); + assertThat(wrapper.getZonedDateTime()).isEqualTo(ZONED_DATE_TIME); + } - new ZonedDateTimeDeserializer().parse(jsonParser); + static final class ZonedDateTimeWrapper { + ZonedDateTime zonedDateTime; + + public ZonedDateTime getZonedDateTime() { + return zonedDateTime; + } + + public void setZonedDateTime(ZonedDateTime zonedDateTime) { + this.zonedDateTime = zonedDateTime; + } } }