DATAMONGO-382 - Fixed potential ClassCastException in MappingMongoConverter.
MappingMongoConverter's convertToMongoType(…) now deals with Sets (and more generally all Collections) correctly.
This commit is contained in:
@@ -813,14 +813,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
return result;
|
||||
}
|
||||
|
||||
if (obj instanceof List) {
|
||||
return maybeConvertList((List<?>) obj);
|
||||
}
|
||||
|
||||
if (obj.getClass().isArray()) {
|
||||
return maybeConvertList(Arrays.asList((Object[]) obj));
|
||||
}
|
||||
|
||||
if (obj instanceof Collection) {
|
||||
return maybeConvertList((Collection<?>) obj);
|
||||
}
|
||||
|
||||
DBObject newDbo = new BasicDBObject();
|
||||
this.write(obj, newDbo);
|
||||
return removeTypeInfoRecursively(newDbo);
|
||||
|
||||
@@ -998,6 +998,24 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(result.containsKey("foobar"), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-382
|
||||
*/
|
||||
@Test
|
||||
public void convertsSetToBasicDBList() {
|
||||
|
||||
Address address = new Address();
|
||||
address.city = "London";
|
||||
address.street = "Foo";
|
||||
|
||||
Object result = converter.convertToMongoType(Collections.singleton(address));
|
||||
assertThat(result, is(BasicDBList.class));
|
||||
|
||||
Set<?> readResult = converter.read(Set.class, (BasicDBList) result);
|
||||
assertThat(readResult.size(), is(1));
|
||||
assertThat(readResult.iterator().next(), is(Map.class));
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user