Skip to content

Commit

Permalink
Fix converter registration when using driver native time codec.
Browse files Browse the repository at this point in the history
This commit prevents converters from being used as writing converter causing asymmetric write/read operations.

Closes #4390
Original pull request: #4392
  • Loading branch information
christophstrobl authored and mp911de committed Jun 14, 2023
1 parent 4d51d27 commit 29021d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.data.convert.PropertyValueConverter;
import org.springframework.data.convert.PropertyValueConverterFactory;
import org.springframework.data.convert.PropertyValueConverterRegistrar;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.SimplePropertyValueConversions;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.mapping.model.SimpleTypeHolder;
Expand Down Expand Up @@ -361,6 +362,7 @@ ConverterConfiguration createConverterConfiguration() {
}, this.propertyValueConversions);
}

@ReadingConverter
private enum DateToUtcLocalDateTimeConverter implements Converter<Date, LocalDateTime> {
INSTANCE;

Expand All @@ -370,6 +372,7 @@ public LocalDateTime convert(Date source) {
}
}

@ReadingConverter
private enum DateToUtcLocalTimeConverter implements Converter<Date, LocalTime> {
INSTANCE;

Expand All @@ -379,6 +382,7 @@ public LocalTime convert(Date source) {
}
}

@ReadingConverter
private enum DateToUtcLocalDateConverter implements Converter<Date, LocalDate> {
INSTANCE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
import org.springframework.data.mongodb.core.aggregation.StringOperators;
import org.springframework.data.mongodb.core.convert.LazyLoadingProxy;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions.MongoConverterConfigurationAdapter;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.data.mongodb.core.index.IndexField;
Expand Down Expand Up @@ -1789,6 +1791,30 @@ public void findsEntityByDateReference() {
assertThat(result.get(0).date).isNotNull();
}

@Test // GH-4390
void nativeDriverDateTimeCodecShouldBeApplied/*when configured*/() {

MongoTestTemplate ops = new MongoTestTemplate(cfg -> {
cfg.configureConversion(conversion -> {
conversion.customConversions(
MongoCustomConversions.create(MongoConverterConfigurationAdapter::useNativeDriverJavaTimeCodecs));
});
});

TypeWithDate source = new TypeWithDate();
source.id = "id-1";
source.date = Date.from(Instant.now());

ops.save(source);

var dbDate = ops.execute(TypeWithDate.class,
collection -> collection.find(new org.bson.Document("_id", source.id)).first().get("date"));

TypeWithDate target = ops.findOne(query(where("date").is(source.date)), TypeWithDate.class);

assertThat(target.date).isEqualTo(source.date).isEqualTo(dbDate);
}

@Test // DATAMONGO-540
public void findOneAfterUpsertForNonExistingObjectReturnsTheInsertedObject() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ void propertyValueConverterRegistrationWorksAsExpected() {
assertThat(conversions.getPropertyValueConversions().hasValueConverter(persistentProperty)).isTrue();
}

@Test // GH-4390
void doesNotReturnConverterForNativeTimeTimeIfUsingDriverCodec() {

MongoCustomConversions conversions = MongoCustomConversions.create(config -> {
config.useNativeDriverJavaTimeCodecs();
});

assertThat(conversions.getCustomWriteTarget(Date.class)).isEmpty();
}

static class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

@Override
Expand Down

0 comments on commit 29021d1

Please sign in to comment.