Skip to content

DATAMONGO-2328 - Add missing target type conversions for field level type hints. #773

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>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2328-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>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2328-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 @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2328-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 @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2328-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package org.springframework.data.mongodb.core.convert;

import static org.springframework.data.convert.ConverterBuilder.*;

import java.math.BigInteger;
import java.util.Date;

import org.bson.types.Code;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.ConversionService;
Expand Down Expand Up @@ -93,6 +97,21 @@ private void initializeConverters() {
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
}

if (!conversionService.canConvert(Date.class, Long.class)) {
conversionService.addConverter(writing(Date.class, Long.class, Date::getTime).getWritingConverter());
}

if (!conversionService.canConvert(Long.class, Date.class)) {
conversionService.addConverter(reading(Long.class, Date.class, Date::new).getReadingConverter());
}

if (!conversionService.canConvert(ObjectId.class, Date.class)) {

conversionService.addConverter(
reading(ObjectId.class, Date.class, objectId -> new Date(objectId.getTimestamp())).getReadingConverter());
}

conversionService.addConverter(reading(Code.class, String.class, Code::getCode).getReadingConverter());
conversions.registerConvertersIn(conversionService);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -1943,6 +1942,16 @@ public void mapsValueToExplicitTargetType() {
assertThat(target.get("script")).isEqualTo(new Code(source.script));
}

@Test // DATAMONGO-2328
public void readsScriptAsStringWhenAnnotatedWithFieldTargetType() {

String reference = "if (a > b) a else b";
WithExplicitTargetTypes target = converter.read(WithExplicitTargetTypes.class,
new org.bson.Document("script", new Code(reference)));

assertThat(target.script).isEqualTo(reference);
}

@Test // DATAMONGO-1849
public void mapsCollectionValueToExplicitTargetType() {

Expand All @@ -1968,6 +1977,96 @@ public void mapsBigDecimalToDecimal128WhenAnnotatedWithFieldTargetType() {
assertThat(target.get("bigDecimal")).isEqualTo(new Decimal128(source.bigDecimal));
}

@Test // DATAMONGO-2328
public void mapsDateToLongWhenAnnotatedWithFieldTargetType() {

WithExplicitTargetTypes source = new WithExplicitTargetTypes();
source.dateAsLong = new Date();

org.bson.Document target = new org.bson.Document();
converter.write(source, target);

assertThat(target.get("dateAsLong")).isEqualTo(source.dateAsLong.getTime());
}

@Test // DATAMONGO-2328
public void readsLongAsDateWhenAnnotatedWithFieldTargetType() {

Date reference = new Date();
WithExplicitTargetTypes target = converter.read(WithExplicitTargetTypes.class,
new org.bson.Document("dateAsLong", reference.getTime()));

assertThat(target.dateAsLong).isEqualTo(reference);
}

@Test // DATAMONGO-2328
public void mapsLongToDateWhenAnnotatedWithFieldTargetType() {

Date date = new Date();
WithExplicitTargetTypes source = new WithExplicitTargetTypes();
source.longAsDate = date.getTime();

org.bson.Document target = new org.bson.Document();
converter.write(source, target);

assertThat(target.get("longAsDate")).isEqualTo(date);
}

@Test // DATAMONGO-2328
public void readsDateAsLongWhenAnnotatedWithFieldTargetType() {

Date reference = new Date();
WithExplicitTargetTypes target = converter.read(WithExplicitTargetTypes.class,
new org.bson.Document("longAsDate", reference));

assertThat(target.longAsDate).isEqualTo(reference.getTime());
}

@Test // DATAMONGO-2328
public void mapsStringAsBooleanWhenAnnotatedWithFieldTargetType() {

WithExplicitTargetTypes source = new WithExplicitTargetTypes();
source.stringAsBoolean = "true";

org.bson.Document target = new org.bson.Document();
converter.write(source, target);

assertThat(target.get("stringAsBoolean")).isEqualTo(true);
}

@Test // DATAMONGO-2328
public void readsBooleanAsStringWhenAnnotatedWithFieldTargetType() {

WithExplicitTargetTypes target = converter.read(WithExplicitTargetTypes.class,
new org.bson.Document("stringAsBoolean", true));

assertThat(target.stringAsBoolean).isEqualTo("true");
}

@Test // DATAMONGO-2328
public void mapsDateAsObjectIdWhenAnnotatedWithFieldTargetType() {

WithExplicitTargetTypes source = new WithExplicitTargetTypes();
source.dateAsObjectId = new Date();

org.bson.Document target = new org.bson.Document();
converter.write(source, target);

// need to compare the the timestamp as ObjectId has an internal counter
assertThat(target.get("dateAsObjectId", ObjectId.class).getTimestamp())
.isEqualTo(new ObjectId(source.dateAsObjectId).getTimestamp());
}

@Test // DATAMONGO-2328
public void readsObjectIdAsDateWhenAnnotatedWithFieldTargetType() {

ObjectId reference = new ObjectId();
WithExplicitTargetTypes target = converter.read(WithExplicitTargetTypes.class,
new org.bson.Document("dateAsObjectId", reference));

assertThat(target.dateAsObjectId).isEqualTo(new Date(reference.getTimestamp()));
}

static class GenericType<T> {
T content;
}
Expand All @@ -1987,9 +2086,7 @@ void method() {}
},
SECOND {
@Override
void method() {

}
void method() {}
};

abstract void method();
Expand Down Expand Up @@ -2416,7 +2513,20 @@ static class WithExplicitTargetTypes {
@Field(targetType = FieldType.SCRIPT) //
List<String> scripts;

@Field(targetType = FieldType.DECIMAL128) BigDecimal bigDecimal;
@Field(targetType = FieldType.DECIMAL128) //
BigDecimal bigDecimal;

@Field(targetType = FieldType.INT64) //
Date dateAsLong;

@Field(targetType = FieldType.DATE_TIME) //
Long longAsDate;

@Field(targetType = FieldType.BOOLEAN) //
String stringAsBoolean;

@Field(targetType = FieldType.OBJECT_ID) //
Date dateAsObjectId;
}

}