Skip to content

Commit 93c9713

Browse files
committed
DATAMONGO-382 - Fixed potential ClassCastException in MappingMongoConverter.
MappingMongoConverter's convertToMongoType(…) now deals with Sets (and more generally all Collections) correctly.
1 parent 68a0c1e commit 93c9713

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,14 @@ public Object convertToMongoType(Object obj) {
813813
return result;
814814
}
815815

816-
if (obj instanceof List) {
817-
return maybeConvertList((List<?>) obj);
818-
}
819-
820816
if (obj.getClass().isArray()) {
821817
return maybeConvertList(Arrays.asList((Object[]) obj));
822818
}
823819

820+
if (obj instanceof Collection) {
821+
return maybeConvertList((Collection<?>) obj);
822+
}
823+
824824
DBObject newDbo = new BasicDBObject();
825825
this.write(obj, newDbo);
826826
return removeTypeInfoRecursively(newDbo);

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

+18
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,24 @@ public void unescapesDotInMapKeysIfReplacementConfigured() {
998998
assertThat(result.containsKey("foobar"), is(false));
999999
}
10001000

1001+
/**
1002+
* @see DATAMONGO-382
1003+
*/
1004+
@Test
1005+
public void convertsSetToBasicDBList() {
1006+
1007+
Address address = new Address();
1008+
address.city = "London";
1009+
address.street = "Foo";
1010+
1011+
Object result = converter.convertToMongoType(Collections.singleton(address));
1012+
assertThat(result, is(BasicDBList.class));
1013+
1014+
Set<?> readResult = converter.read(Set.class, (BasicDBList) result);
1015+
assertThat(readResult.size(), is(1));
1016+
assertThat(readResult.iterator().next(), is(Map.class));
1017+
}
1018+
10011019
static class GenericType<T> {
10021020
T content;
10031021
}

0 commit comments

Comments
 (0)