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:
Oliver Gierke
2012-03-06 21:26:10 +01:00
parent 68a0c1ee2c
commit 93c9713adf
2 changed files with 22 additions and 4 deletions

View File

@@ -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);

View File

@@ -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;
}