diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java index d8530dcc4..501d19f4d 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2015 by the original author(s). + * Copyright 2011-2016 by the original author(s). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -1001,11 +1002,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App } if (obj instanceof Map) { - DBObject result = new BasicDBObject(); + Map converted = new LinkedHashMap(); + for (Map.Entry entry : ((Map) obj).entrySet()) { - result.put(entry.getKey().toString(), convertToMongoType(entry.getValue(), typeHint)); + converted.put(convertToMongoType(entry.getKey()), convertToMongoType(entry.getValue(), + typeHint != null && typeHint.getMapValueType() != null ? typeHint.getMapValueType() : typeHint)); } - return result; + return new BasicDBObject(converted); } if (obj.getClass().isArray()) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index feca9d76b..f0ec0c22b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java @@ -887,6 +887,32 @@ public class UpdateMapperUnitTests { assertThat($set.get("primIntValue"), Is. is(10)); } + /** + * @see DATAMONGO-1423 + */ + @Test + public void mappingShouldConsiderCustomConvertersForEnumMapKeys() { + + CustomConversions conversions = new CustomConversions( + Arrays.asList(AllocationToStringConverter.INSTANCE, StringToAllocationConverter.INSTANCE)); + + MongoMappingContext mappingContext = new MongoMappingContext(); + mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); + mappingContext.afterPropertiesSet(); + + MappingMongoConverter converter = new MappingMongoConverter(mock(DbRefResolver.class), mappingContext); + converter.setCustomConversions(conversions); + converter.afterPropertiesSet(); + + UpdateMapper mapper = new UpdateMapper(converter); + + Update update = new Update().set("enumAsMapKey", Collections.singletonMap(Allocation.AVAILABLE, 100)); + DBObject result = mapper.getMappedObject(update.getUpdateObject(), + mappingContext.getPersistentEntity(ClassWithEnum.class)); + + assertThat(result, isBsonObject().containing("$set.enumAsMapKey.V", 100)); + } + static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes { ListModelWrapper concreteTypeWithListAttributeOfInterfaceType; } @@ -1113,6 +1139,7 @@ public class UpdateMapperUnitTests { static class ClassWithEnum { Allocation allocation; + Map enumAsMapKey; static enum Allocation {