Browse Source

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.
pull/410/head
Christoph Strobl 9 years ago committed by Oliver Gierke
parent
commit
2ca3df1ff4
  1. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

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

@ -1012,7 +1012,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App @@ -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);

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

@ -914,6 +914,24 @@ public class UpdateMapperUnitTests { @@ -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;
}

Loading…
Cancel
Save