Skip to content

Commit b35f151

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 17afb07 commit b35f151

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
@@ -887,6 +887,32 @@ public void mapsAtomicIntegerToPrimitiveIntegerCorrectly() {
887887
assertThat($set.get("primIntValue"), Is.<Object> is(10));
888888
}
889889

890+
/**
891+
* @see DATAMONGO-1423
892+
*/
893+
@Test
894+
public void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
895+
896+
CustomConversions conversions = new CustomConversions(
897+
Arrays.asList(AllocationToStringConverter.INSTANCE, StringToAllocationConverter.INSTANCE));
898+
899+
MongoMappingContext mappingContext = new MongoMappingContext();
900+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
901+
mappingContext.afterPropertiesSet();
902+
903+
MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
904+
converter.setCustomConversions(conversions);
905+
converter.afterPropertiesSet();
906+
907+
UpdateMapper mapper = new UpdateMapper(converter);
908+
909+
Update update = new Update().set("enumAsMapKey", Collections.singletonMap(Allocation.AVAILABLE, 100));
910+
DBObject result = mapper.getMappedObject(update.getUpdateObject(),
911+
mappingContext.getPersistentEntity(ClassWithEnum.class));
912+
913+
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
914+
}
915+
890916
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
891917
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
892918
}
@@ -1113,6 +1139,7 @@ static class EntityWithObjectMap {
11131139
static class ClassWithEnum {
11141140

11151141
Allocation allocation;
1142+
Map<Allocation, String> enumAsMapKey;
11161143

11171144
static enum Allocation {
11181145

0 commit comments

Comments
 (0)