customize object mapper from the object mapper factory (#382)

* customize object mapper from the object mapper factory

* remove objectmapper instance creation from tests

* Revert "remove ZoneDateTimeDeserializer. Redunandant of JavaTimeModule from Jackson"
This commit is contained in:
kxmas 2019-09-24 15:37:39 -05:00 коммит произвёл Kushagra Thapar
Родитель 3ae374f0f8
Коммит 488660839d
7 изменённых файлов: 35 добавлений и 77 удалений

Просмотреть файл

@ -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> R readInternal(final DocumentDbPersistentEntity<?> entity, Class<R> 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) {

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -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<ZonedDateTime> {
@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);
}
}
}

Просмотреть файл

@ -66,7 +66,6 @@ public class DocumentDbTemplateIT {
private DocumentDbTemplate dbTemplate;
private MappingDocumentDbConverter dbConverter;
private DocumentDbMappingContext mappingContext;
private ObjectMapper objectMapper;
private DocumentCollection collectionPerson;
private DocumentDbEntityInformation<Person, String> 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);

Просмотреть файл

@ -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();

Просмотреть файл

@ -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<Person, String> 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();

Просмотреть файл

@ -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;
}
}
}