Skip to content

Commit 1c86166

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 40567df commit 1c86166

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
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-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.junit.jupiter.api.AfterEach;
4747
import org.junit.jupiter.api.Test;
4848
import org.junit.jupiter.api.extension.ExtendWith;
49-
5049
import org.springframework.context.ConfigurableApplicationContext;
5150
import org.springframework.context.support.GenericApplicationContext;
5251
import org.springframework.core.convert.converter.Converter;
@@ -69,6 +68,8 @@
6968
import org.springframework.data.mongodb.MongoDatabaseFactory;
7069
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
7170
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
71+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
72+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
7273
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
7374
import org.springframework.data.mongodb.core.index.Index;
7475
import org.springframework.data.mongodb.core.index.IndexField;
@@ -1789,6 +1790,30 @@ public void findsEntityByDateReference() {
17891790
assertThat(result.get(0).date).isNotNull();
17901791
}
17911792

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

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)