Skip to content

Fix converter registration when using driver native time codec. #4392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4390-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4390-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4390-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.0-SNAPSHOT</version>
<version>4.2.x-4390-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
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,31 @@ 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