Browse Source

Added handling of BigInteger.

Added converter to handle BigInteger values. Adapted id handling to try converting and object to String before taking the id as is. Made custom converter implementations safe against invocations with null.
pull/1/head
Oliver Gierke 15 years ago
parent
commit
a5328da460
  1. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java
  2. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  3. 29
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java
  4. 19
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

8
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/AbstractMongoConverter.java

@ -29,8 +29,10 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToObjectIdConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToObjectIdConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.BigIntegerToStringConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToBigIntegerConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToBigIntegerConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToStringConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.ObjectIdToStringConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigIntegerConverter;
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToObjectIdConverter; import org.springframework.data.mongodb.core.convert.MongoConverters.StringToObjectIdConverter;
import com.mongodb.BasicDBList; import com.mongodb.BasicDBList;
@ -87,6 +89,12 @@ public abstract class AbstractMongoConverter implements MongoConverter, Initiali
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) { if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE); conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
} }
if (!conversionService.canConvert(BigInteger.class, String.class)) {
conversionService.addConverter(BigIntegerToStringConverter.INSTANCE);
}
if (!conversionService.canConvert(String.class, BigInteger.class)) {
conversionService.addConverter(StringToBigIntegerConverter.INSTANCE);
}
conversions.registerConvertersIn(conversionService); conversions.registerConvertersIn(conversionService);
} }

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -357,10 +357,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
final MongoPersistentProperty idProperty = entity.getIdProperty(); final MongoPersistentProperty idProperty = entity.getIdProperty();
if (!dbo.containsField("_id") && null != idProperty) { if (!dbo.containsField("_id") && null != idProperty) {
Object idObj = null; Object idObj = null;
Class<?>[] targetClasses = new Class<?>[] { ObjectId.class, Object.class }; Class<?>[] targetClasses = new Class<?>[] { ObjectId.class, String.class, Object.class };
for (Class<?> targetClasse : targetClasses) { for (Class<?> targetClass : targetClasses) {
try { try {
idObj = wrapper.getProperty(idProperty, targetClasse, useFieldAccessOnly); idObj = wrapper.getProperty(idProperty, targetClass, useFieldAccessOnly);
if (null != idObj) { if (null != idObj) {
break; break;
} }

29
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoConverters.java

@ -20,6 +20,7 @@ import java.math.BigInteger;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
/** /**
* Wrapper class to contain useful converters for the usage with Mongo. * Wrapper class to contain useful converters for the usage with Mongo.
@ -44,7 +45,7 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public String convert(ObjectId id) { public String convert(ObjectId id) {
return id.toString(); return id == null ? null : id.toString();
} }
} }
@ -57,7 +58,7 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public ObjectId convert(String source) { public ObjectId convert(String source) {
return new ObjectId(source); return StringUtils.hasText(source) ? new ObjectId(source) : null;
} }
} }
@ -70,7 +71,7 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public BigInteger convert(ObjectId source) { public BigInteger convert(ObjectId source) {
return new BigInteger(source.toString(), 16); return source == null ? null : new BigInteger(source.toString(), 16);
} }
} }
@ -83,7 +84,7 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public ObjectId convert(BigInteger source) { public ObjectId convert(BigInteger source) {
return new ObjectId(source.toString(16)); return source == null ? null : new ObjectId(source.toString(16));
} }
} }
@ -92,7 +93,7 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public String convert(BigDecimal source) { public String convert(BigDecimal source) {
return source.toString(); return source == null ? null : source.toString();
} }
} }
@ -100,7 +101,23 @@ abstract class MongoConverters {
INSTANCE; INSTANCE;
public BigDecimal convert(String source) { public BigDecimal convert(String source) {
return new BigDecimal(source); return StringUtils.hasText(source) ? new BigDecimal(source) : null;
}
}
public static enum BigIntegerToStringConverter implements Converter<BigInteger, String> {
INSTANCE;
public String convert(BigInteger source) {
return source == null ? null : source.toString();
}
}
public static enum StringToBigIntegerConverter implements Converter<String, BigInteger> {
INSTANCE;
public BigInteger convert(String source) {
return StringUtils.hasText(source) ? new BigInteger(source) : null;
} }
} }
} }

19
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -40,6 +41,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions; import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
@ -508,6 +510,18 @@ public class MappingMongoConverterUnitTests {
assertThat(((DBObject) map).keySet(), hasItem("en_US")); assertThat(((DBObject) map).keySet(), hasItem("en_US"));
} }
@Test
public void writesBigIntegerIdCorrectly() {
ClassWithBigIntegerId foo = new ClassWithBigIntegerId();
foo.id = BigInteger.valueOf(23L);
DBObject result = new BasicDBObject();
converter.write(foo, result);
assertThat(result.get("_id"), is(instanceOf(String.class)));
}
class GenericType<T> { class GenericType<T> {
T content; T content;
} }
@ -571,6 +585,11 @@ public class MappingMongoConverterUnitTests {
Locale locale; Locale locale;
} }
class ClassWithBigIntegerId {
@Id
BigInteger id;
}
private class LocalDateToDateConverter implements Converter<LocalDate, Date> { private class LocalDateToDateConverter implements Converter<LocalDate, Date> {
public Date convert(LocalDate source) { public Date convert(LocalDate source) {

Loading…
Cancel
Save