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.
This commit is contained in:
Christoph Strobl
2016-09-08 09:30:20 +02:00
committed by Oliver Gierke
parent a8751249fd
commit 2ca3df1ff4
2 changed files with 20 additions and 1 deletions

View File

@@ -1012,7 +1012,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
TypeInformation<? extends Object> valueTypeHint = typeHint != null && typeHint.getMapValueType() != null
? typeHint.getMapValueType() : typeHint;
converted.put(convertToMongoType(entry.getKey()), convertToMongoType(entry.getValue(), valueTypeHint));
converted.put(getPotentiallyConvertedSimpleWrite(entry.getKey()).toString(),
convertToMongoType(entry.getValue(), valueTypeHint));
}
return new BasicDBObject(converted);

View File

@@ -914,6 +914,24 @@ public class UpdateMapperUnitTests {
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
}
/**
* @see DATAMONGO-1486
*/
@Test
public void mappingShouldConvertMapKeysToString() {
Update update = new Update().set("map", Collections.singletonMap(25, "#StarTrek50"));
DBObject mappedUpdate = mapper.getMappedObject(update.getUpdateObject(),
context.getPersistentEntity(EntityWithObjectMap.class));
DBObject $set = getAsDBObject(mappedUpdate, "$set");
DBObject mapToSet = getAsDBObject($set, "map");
for (Object key : mapToSet.keySet()) {
assertThat(key, instanceOf(String.class));
}
}
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
}