Skip to content

Commit 2d4082a

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1423 - Map keys now get registered conversions applied for Updates.
We now pipe map keys through the potentially registered conversions when mapping Updates. Orignal pull request: #365.
1 parent b2cd7bb commit 2d4082a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 by the original author(s).
2+
* Copyright 2011-2016 by the original author(s).
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.HashSet;
23+
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Map.Entry;
@@ -1001,11 +1002,13 @@ public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
10011002
}
10021003

10031004
if (obj instanceof Map) {
1004-
DBObject result = new BasicDBObject();
1005+
Map<Object, Object> converted = new LinkedHashMap<Object, Object>();
1006+
10051007
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
1006-
result.put(entry.getKey().toString(), convertToMongoType(entry.getValue(), typeHint));
1008+
converted.put(convertToMongoType(entry.getKey()), convertToMongoType(entry.getValue(),
1009+
typeHint != null && typeHint.getMapValueType() != null ? typeHint.getMapValueType() : typeHint));
10071010
}
1008-
return result;
1011+
return new BasicDBObject(converted);
10091012
}
10101013

10111014
if (obj.getClass().isArray()) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,32 @@ public void mapsNullValueCorrectlyForPropertyOfNestedDocument() {
857857
assertThat($set.get("concreteValue.name"), nullValue());
858858
}
859859

860+
/**
861+
* @see DATAMONGO-1423
862+
*/
863+
@Test
864+
public void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
865+
866+
CustomConversions conversions = new CustomConversions(
867+
Arrays.asList(AllocationToStringConverter.INSTANCE, StringToAllocationConverter.INSTANCE));
868+
869+
MongoMappingContext mappingContext = new MongoMappingContext();
870+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
871+
mappingContext.afterPropertiesSet();
872+
873+
MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
874+
converter.setCustomConversions(conversions);
875+
converter.afterPropertiesSet();
876+
877+
UpdateMapper mapper = new UpdateMapper(converter);
878+
879+
Update update = new Update().set("enumAsMapKey", Collections.singletonMap(Allocation.AVAILABLE, 100));
880+
DBObject result = mapper.getMappedObject(update.getUpdateObject(),
881+
mappingContext.getPersistentEntity(ClassWithEnum.class));
882+
883+
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
884+
}
885+
860886
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
861887
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
862888
}
@@ -1083,6 +1109,7 @@ static class EntityWithObjectMap {
10831109
static class ClassWithEnum {
10841110

10851111
Allocation allocation;
1112+
Map<Allocation, String> enumAsMapKey;
10861113

10871114
static enum Allocation {
10881115

0 commit comments

Comments
 (0)