Skip to content

Commit 395bb1f

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1486 - Fix ClassCastException when mapping non-String Map key for updates.
We now make sure to convert Map keys into Strings when mapping update values for Map properties. Original pull request: #387.
1 parent eb1392c commit 395bb1f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,8 @@ public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
10291029
TypeInformation<? extends Object> valueTypeHint = typeHint != null && typeHint.getMapValueType() != null
10301030
? typeHint.getMapValueType() : typeHint;
10311031

1032-
converted.put(convertToMongoType(entry.getKey()), convertToMongoType(entry.getValue(), valueTypeHint));
1032+
converted.put(getPotentiallyConvertedSimpleWrite(entry.getKey()).toString(),
1033+
convertToMongoType(entry.getValue(), valueTypeHint));
10331034
}
10341035

10351036
return new BasicDBObject(converted);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,24 @@ public void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
982982
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
983983
}
984984

985+
/**
986+
* @see DATAMONGO-1486
987+
*/
988+
@Test
989+
public void mappingShouldConvertMapKeysToString() {
990+
991+
Update update = new Update().set("map", Collections.singletonMap(25, "#StarTrek50"));
992+
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
993+
context.getPersistentEntity(EntityWithObjectMap.class));
994+
995+
DBObject $set = getAsDBObject(mappedUpdate, "$set");
996+
DBObject mapToSet = getAsDBObject($set, "map");
997+
998+
for (Object key : mapToSet.keySet()) {
999+
assertThat(key, instanceOf(String.class));
1000+
}
1001+
}
1002+
9851003
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
9861004
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
9871005
}

0 commit comments

Comments
 (0)