Skip to content

Commit 5f9b0f0

Browse files
christophstroblmp911de
authored andcommitted
Fix converter registration when using driver native time codec.
This commit prevents converters from being used as writing converter causing asymmetric write/read operations. Closes #4390 Original pull request: #4392
1 parent 37fb6f5 commit 5f9b0f0

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoCustomConversions.java

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.convert.PropertyValueConverter;
4343
import org.springframework.data.convert.PropertyValueConverterFactory;
4444
import org.springframework.data.convert.PropertyValueConverterRegistrar;
45+
import org.springframework.data.convert.ReadingConverter;
4546
import org.springframework.data.convert.SimplePropertyValueConversions;
4647
import org.springframework.data.convert.WritingConverter;
4748
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -370,6 +371,7 @@ ConverterConfiguration createConverterConfiguration() {
370371
}, this.propertyValueConversions);
371372
}
372373

374+
@ReadingConverter
373375
private enum DateToUtcLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
374376
INSTANCE;
375377

@@ -379,6 +381,7 @@ public LocalDateTime convert(Date source) {
379381
}
380382
}
381383

384+
@ReadingConverter
382385
private enum DateToUtcLocalTimeConverter implements Converter<Date, LocalTime> {
383386
INSTANCE;
384387

@@ -388,6 +391,7 @@ public LocalTime convert(Date source) {
388391
}
389392
}
390393

394+
@ReadingConverter
391395
private enum DateToUtcLocalDateConverter implements Converter<Date, LocalDate> {
392396
INSTANCE;
393397

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@
3939
import java.util.stream.Collectors;
4040
import java.util.stream.IntStream;
4141

42-
import org.bson.Document;
4342
import org.bson.types.ObjectId;
4443
import org.joda.time.DateTime;
4544
import org.junit.jupiter.api.AfterEach;
4645
import org.junit.jupiter.api.Test;
4746
import org.junit.jupiter.api.extension.ExtendWith;
48-
4947
import org.springframework.context.ConfigurableApplicationContext;
5048
import org.springframework.context.support.GenericApplicationContext;
5149
import org.springframework.core.convert.converter.Converter;
@@ -67,6 +65,8 @@
6765
import org.springframework.data.mongodb.MongoDatabaseFactory;
6866
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
6967
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
68+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
69+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
7070
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
7171
import org.springframework.data.mongodb.core.index.Index;
7272
import org.springframework.data.mongodb.core.index.IndexField;
@@ -1787,6 +1787,30 @@ public void findsEntityByDateReference() {
17871787
assertThat(result.get(0).date).isNotNull();
17881788
}
17891789

1790+
@Test // GH-4390
1791+
void nativeDriverDateTimeCodecShouldBeApplied/*when configured*/() {
1792+
1793+
MongoTestTemplate ops = new MongoTestTemplate(cfg -> {
1794+
cfg.configureConversion(conversion -> {
1795+
conversion.customConversions(
1796+
MongoCustomConversions.create(MongoConverterConfigurationAdapter::useNativeDriverJavaTimeCodecs));
1797+
});
1798+
});
1799+
1800+
TypeWithDate source = new TypeWithDate();
1801+
source.id = "id-1";
1802+
source.date = Date.from(Instant.now());
1803+
1804+
ops.save(source);
1805+
1806+
Object dbDate = ops.execute(TypeWithDate.class,
1807+
collection -> collection.find(new org.bson.Document("_id", source.id)).first().get("date"));
1808+
1809+
TypeWithDate target = ops.findOne(query(where("date").is(source.date)), TypeWithDate.class);
1810+
1811+
assertThat(target.date).isEqualTo(source.date).isEqualTo(dbDate);
1812+
}
1813+
17901814
@Test // DATAMONGO-540
17911815
public void findOneAfterUpsertForNonExistingObjectReturnsTheInsertedObject() {
17921816

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MongoCustomConversionsUnitTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ void propertyValueConverterRegistrationWorksAsExpected() {
6464
assertThat(conversions.getPropertyValueConversions().hasValueConverter(persistentProperty)).isTrue();
6565
}
6666

67+
@Test // GH-4390
68+
void doesNotReturnConverterForNativeTimeTimeIfUsingDriverCodec() {
69+
70+
MongoCustomConversions conversions = MongoCustomConversions.create(config -> {
71+
config.useNativeDriverJavaTimeCodecs();
72+
});
73+
74+
assertThat(conversions.getCustomWriteTarget(Date.class)).isEmpty();
75+
}
76+
6777
static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
6878

6979
@Override

0 commit comments

Comments
 (0)