From 93e911985e840642d0b2c1ee2f267d3b3df7c9e4 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 21 Aug 2019 10:25:32 +0200 Subject: [PATCH] DATAMONGO-2349 - Fix converter registration for java.time types. The MongoDB Java Driver does not handle java.time types. Therefore those must not be considered simple types. The behavior was changed by DATACMNS-1294 forcing usage of Reading & WritingConverter annotations to disambiguate converter direction. This commit restores the converter registration to the state before the change in Spring Data Commons. Original pull request: #783. --- .../core/mapping/MongoSimpleTypes.java | 13 ++++- .../MongoCustomConversionsUnitTests.java | 49 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoCustomConversionsUnitTests.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java index b22be6280..d029c7d04 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java @@ -82,7 +82,18 @@ public abstract class MongoSimpleTypes { } private static final Set> MONGO_SIMPLE_TYPES; - public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true); + public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true) { + + @Override + public boolean isSimpleType(Class type) { + + if (type.getName().startsWith("java.time")) { + return false; + } + + return super.isSimpleType(type); + } + }; private MongoSimpleTypes() {} } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoCustomConversionsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoCustomConversionsUnitTests.java new file mode 100644 index 000000000..3abe9cf04 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoCustomConversionsUnitTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2019 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 + * + * https://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.data.mongodb.core.convert; + +import static org.assertj.core.api.Assertions.*; + +import java.time.ZonedDateTime; +import java.util.Collections; +import java.util.Date; + +import org.junit.Test; +import org.springframework.core.convert.converter.Converter; + +/** + * @author Christoph Strobl + */ +public class MongoCustomConversionsUnitTests { + + @Test // DATAMONGO-2349 + public void nonAnnotatedConverterForJavaTimeTypeShouldOnlyBeRegisteredAsReadingConverter() { + + MongoCustomConversions conversions = new MongoCustomConversions( + Collections.singletonList(new DateToZonedDateTimeConverter())); + + assertThat(conversions.hasCustomReadTarget(Date.class, ZonedDateTime.class)).isTrue(); + assertThat(conversions.hasCustomWriteTarget(Date.class)).isFalse(); + } + + static class DateToZonedDateTimeConverter implements Converter { + + @Override + public ZonedDateTime convert(Date source) { + return ZonedDateTime.now(); + } + } +}