Skip to content

Commit 8c66269

Browse files
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 7746f85 commit 8c66269

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 8 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;
@@ -1097,11 +1098,14 @@ public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
10971098
}
10981099

10991100
if (obj instanceof Map) {
1100-
Document result = new Document();
1101+
1102+
Map<String, Object> converted = new LinkedHashMap<String, Object>();
1103+
11011104
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
1102-
result.put(entry.getKey().toString(), convertToMongoType(entry.getValue(), typeHint));
1105+
converted.put(convertToMongoType(entry.getKey()).toString(), convertToMongoType(entry.getValue(),
1106+
typeHint != null && typeHint.getMapValueType() != null ? typeHint.getMapValueType() : typeHint));
11031107
}
1104-
return result;
1108+
return new Document(converted);
11051109
}
11061110

11071111
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
@@ -907,6 +907,32 @@ public void mapsMaxCorrectly() {
907907
assertThat(mappedUpdate, isBsonObject().containing("$max", new Document("maxfield", 999)));
908908
}
909909

910+
/**
911+
* @see DATAMONGO-1423
912+
*/
913+
@Test
914+
public void mappingShouldConsiderCustomConvertersForEnumMapKeys() {
915+
916+
CustomConversions conversions = new CustomConversions(
917+
Arrays.asList(AllocationToStringConverter.INSTANCE, StringToAllocationConverter.INSTANCE));
918+
919+
MongoMappingContext mappingContext = new MongoMappingContext();
920+
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
921+
mappingContext.afterPropertiesSet();
922+
923+
MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext);
924+
converter.setCustomConversions(conversions);
925+
converter.afterPropertiesSet();
926+
927+
UpdateMapper mapper = new UpdateMapper(converter);
928+
929+
Update update = new Update().set("enumAsMapKey", Collections.singletonMap(Allocation.AVAILABLE, 100));
930+
Document result = mapper.getMappedObject(update.getUpdateObject(),
931+
mappingContext.getPersistentEntity(ClassWithEnum.class));
932+
933+
assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100));
934+
}
935+
910936
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
911937
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
912938
}
@@ -1133,6 +1159,7 @@ static class EntityWithObjectMap {
11331159
static class ClassWithEnum {
11341160

11351161
Allocation allocation;
1162+
Map<Allocation, String> enumAsMapKey;
11361163

11371164
static enum Allocation {
11381165

0 commit comments

Comments
 (0)