Skip to content

Commit 110877e

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 5d57100 commit 110877e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
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
@@ -41,6 +41,7 @@
4141
import org.springframework.data.convert.PropertyValueConverter;
4242
import org.springframework.data.convert.PropertyValueConverterFactory;
4343
import org.springframework.data.convert.PropertyValueConverterRegistrar;
44+
import org.springframework.data.convert.ReadingConverter;
4445
import org.springframework.data.convert.SimplePropertyValueConversions;
4546
import org.springframework.data.convert.WritingConverter;
4647
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -361,6 +362,7 @@ ConverterConfiguration createConverterConfiguration() {
361362
}, this.propertyValueConversions);
362363
}
363364

365+
@ReadingConverter
364366
private enum DateToUtcLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
365367
INSTANCE;
366368

@@ -370,6 +372,7 @@ public LocalDateTime convert(Date source) {
370372
}
371373
}
372374

375+
@ReadingConverter
373376
private enum DateToUtcLocalTimeConverter implements Converter<Date, LocalTime> {
374377
INSTANCE;
375378

@@ -379,6 +382,7 @@ public LocalTime convert(Date source) {
379382
}
380383
}
381384

385+
@ReadingConverter
382386
private enum DateToUtcLocalDateConverter implements Converter<Date, LocalDate> {
383387
INSTANCE;
384388

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

+26
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
7070
import org.springframework.data.mongodb.core.aggregation.StringOperators;
7171
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
72+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
73+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
7274
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
7375
import org.springframework.data.mongodb.core.index.Index;
7476
import org.springframework.data.mongodb.core.index.IndexField;
@@ -1789,6 +1791,30 @@ public void findsEntityByDateReference() {
17891791
assertThat(result.get(0).date).isNotNull();
17901792
}
17911793

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

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)