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.
This commit is contained in:
Christoph Strobl
2019-08-21 10:25:32 +02:00
committed by Mark Paluch
parent e7faa1a1ec
commit 93e911985e
2 changed files with 61 additions and 1 deletions

View File

@@ -82,7 +82,18 @@ public abstract class MongoSimpleTypes {
} }
private static final Set<Class<?>> MONGO_SIMPLE_TYPES; private static final Set<Class<?>> 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() {} private MongoSimpleTypes() {}
} }

View File

@@ -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<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date source) {
return ZonedDateTime.now();
}
}
}