Skip to content

Commit 7cf69c5

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1772 - Fix UpdateMapper type key rendering for abstract list elements contained in concrete typed ones.
Original pull request: spring-projects#497.
1 parent 995e5bf commit 7cf69c5

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2016 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
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.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.core.convert;
1717

18+
import java.util.Collection;
1819
import java.util.Map.Entry;
1920

2021
import org.bson.Document;
@@ -226,6 +227,10 @@ private TypeInformation<?> getTypeHintForEntity(@Nullable Object source, MongoPe
226227
return info;
227228
}
228229

230+
if (source instanceof Collection) {
231+
return NESTED_DOCUMENT;
232+
}
233+
229234
if (!type.equals(source.getClass())) {
230235
return info;
231236
}

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

+50
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,34 @@ public void mappingShouldConvertMapKeysToString() {
932932
}
933933
}
934934

935+
@Test // DATAMONGO-1772
936+
public void mappingShouldAddTypeKeyInListOfInterfaceTypeContainedInConcreteObjectCorrectly() {
937+
938+
ConcreteInner inner = new ConcreteInner();
939+
inner.interfaceTypeList = Collections.singletonList(new SomeInterfaceImpl());
940+
List<ConcreteInner> list = Collections.singletonList(inner);
941+
942+
Document mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(),
943+
context.getPersistentEntity(Outer.class));
944+
945+
assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].interfaceTypeList.[0]._class")
946+
.notContaining("$set.concreteInnerList.[0]._class"));
947+
}
948+
949+
@Test // DATAMONGO-1772
950+
public void mappingShouldAddTypeKeyInListOfAbstractTypeContainedInConcreteObjectCorrectly() {
951+
952+
ConcreteInner inner = new ConcreteInner();
953+
inner.abstractTypeList = Collections.singletonList(new SomeInterfaceImpl());
954+
List<ConcreteInner> list = Collections.singletonList(inner);
955+
956+
Document mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(),
957+
context.getPersistentEntity(Outer.class));
958+
959+
assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].abstractTypeList.[0]._class")
960+
.notContaining("$set.concreteInnerList.[0]._class"));
961+
}
962+
935963
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
936964
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
937965
}
@@ -1218,4 +1246,26 @@ static class SimpleValueHolder {
12181246
Integer intValue;
12191247
int primIntValue;
12201248
}
1249+
1250+
static class Outer {
1251+
List<ConcreteInner> concreteInnerList;
1252+
}
1253+
1254+
static class ConcreteInner {
1255+
List<SomeInterfaceType> interfaceTypeList;
1256+
List<SomeAbstractType> abstractTypeList;
1257+
}
1258+
1259+
interface SomeInterfaceType {
1260+
1261+
}
1262+
1263+
static abstract class SomeAbstractType {
1264+
1265+
}
1266+
1267+
static class SomeInterfaceImpl extends SomeAbstractType implements SomeInterfaceType {
1268+
1269+
}
1270+
12211271
}

0 commit comments

Comments
 (0)