Browse Source

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.
pull/663/head
Christoph Strobl 10 years ago committed by Oliver Gierke
parent
commit
2d4082af02
  1. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 27
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 @@ -1001,11 +1002,13 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
}
if (obj instanceof Map) {
DBObject result = new BasicDBObject();
Map<Object, Object> converted = new LinkedHashMap<Object, Object>();
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) 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()) {

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

@ -857,6 +857,32 @@ public class UpdateMapperUnitTests { @@ -857,6 +857,32 @@ public class UpdateMapperUnitTests {
assertThat($set.get("concreteValue.name"), nullValue());
}
/**
* @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;
}
@ -1083,6 +1109,7 @@ public class UpdateMapperUnitTests { @@ -1083,6 +1109,7 @@ public class UpdateMapperUnitTests {
static class ClassWithEnum {
Allocation allocation;
Map<Allocation, String> enumAsMapKey;
static enum Allocation {

Loading…
Cancel
Save