DATADOC-130 - Conversion of Maps with simple key types works again.
Don't use ConversionService for simple type keys as we unregistered ObjectToStringConverter. This in turn causes Number, Boolean, Locale and the like not being convertible to String anymore as for those types only the Type -> String converter is registered but no corresponding converter back. Opened a ticket for this against Core Spring (SPR-8306).
This commit is contained in:
@@ -306,7 +306,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void write(final Object obj, final DBObject dbo) {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void write(final Object obj, final DBObject dbo) {
|
||||
if (null == obj) {
|
||||
return;
|
||||
}
|
||||
@@ -318,6 +319,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
dbo.putAll(result);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Map.class.isAssignableFrom(obj.getClass())) {
|
||||
writeMapInternal((Map<Object, Object>) obj, dbo);
|
||||
return;
|
||||
}
|
||||
|
||||
PersistentEntity<?> entity = mappingContext.getPersistentEntity(obj.getClass());
|
||||
write(obj, dbo, entity);
|
||||
@@ -508,7 +514,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
Object key = entry.getKey();
|
||||
Object val = entry.getValue();
|
||||
if (isSimpleType(key.getClass())) {
|
||||
String simpleKey = conversionService.convert(key, String.class);
|
||||
// Don't use conversion service here as removal of ObjectToString converter results in some primitive types not
|
||||
// being convertable
|
||||
String simpleKey = key.toString();
|
||||
if (isSimpleType(val.getClass())) {
|
||||
dbo.put(simpleKey, val);
|
||||
} else {
|
||||
|
||||
@@ -20,8 +20,12 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -90,7 +94,20 @@ public class MappingMongoConverterUnitTests {
|
||||
Person result = converter.read(Person.class, dbObject);
|
||||
assertThat(result.birthDate, is(notNullValue()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see DATADOC-130
|
||||
*/
|
||||
@Test
|
||||
public void convertsMapTypeCorrectly() {
|
||||
|
||||
Map<Locale, String> map = Collections.singletonMap(Locale.US, "Foo");
|
||||
|
||||
BasicDBObject dbObject = new BasicDBObject();
|
||||
converter.write(map, dbObject);
|
||||
|
||||
assertThat(dbObject.get(Locale.US.toString()).toString(), is("Foo"));
|
||||
}
|
||||
|
||||
public static class Address {
|
||||
String street;
|
||||
|
||||
Reference in New Issue
Block a user