Skip to content

Commit 0e60630

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 9bc3551 commit 0e60630

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
@@ -912,6 +912,32 @@ public void mapsMaxCorrectly() {
912912
assertThat(mappedUpdate, isBsonObject().containing("$max", new BasicDBObject("maxfield", 999)));
913913
}
914914

915+
/**
916+
* @see DATAMONGO-1423
917+
*/
918+
@Test
919+
public void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
920+
921+
CustomConversions conversions = new CustomConversions(
922+
Arrays.asList(AllocationToStringConverter.INSTANCE, StringToAllocationConverter.INSTANCE));
923+
924+
MongoMappingContext mappingContext = new MongoMappingContext();
925+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
926+
mappingContext.afterPropertiesSet();
927+
928+
MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
929+
converter.setCustomConversions(conversions);
930+
converter.afterPropertiesSet();
931+
932+
UpdateMapper mapper = new UpdateMapper(converter);
933+
934+
Update update = new Update().set("enumAsMapKey", Collections.singletonMap(Allocation.AVAILABLE, 100));
935+
DBObject result = mapper.getMappedObject(update.getUpdateObject(),
936+
mappingContext.getPersistentEntity(ClassWithEnum.class));
937+
938+
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
939+
}
940+
915941
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
916942
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
917943
}
@@ -1138,6 +1164,7 @@ static class EntityWithObjectMap {
11381164
static class ClassWithEnum {
11391165

11401166
Allocation allocation;
1167+
Map<Allocation, String> enumAsMapKey;
11411168

11421169
static enum Allocation {
11431170

0 commit comments

Comments
 (0)