Skip to content

Commit 022d55d

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1772 - Fix UpdateMapper type key rendering for abstract list elements contained in concrete typed ones.
Original pull request: #497.
1 parent 1bee09e commit 022d55d

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

Lines changed: 6 additions & 1 deletion
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.springframework.core.convert.converter.Converter;
@@ -161,6 +162,10 @@ private TypeInformation<?> getTypeHintForEntity(Object source, MongoPersistentEn
161162
return info;
162163
}
163164

165+
if (source instanceof Collection) {
166+
return NESTED_DOCUMENT;
167+
}
168+
164169
if (!type.equals(source.getClass())) {
165170
return info;
166171
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,34 @@ public void mappingShouldConvertMapKeysToString() {
901901
}
902902
}
903903

904+
@Test // DATAMONGO-1772
905+
public void mappingShouldAddTypeKeyInListOfInterfaceTypeContainedInConcreteObjectCorrectly() {
906+
907+
ConcreteInner inner = new ConcreteInner();
908+
inner.interfaceTypeList = Collections.<SomeInterfaceType> singletonList(new SomeInterfaceImpl());
909+
List<ConcreteInner> list = Collections.singletonList(inner);
910+
911+
DBObject mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(),
912+
context.getPersistentEntity(Outer.class));
913+
914+
assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].interfaceTypeList.[0]._class")
915+
.notContaining("$set.concreteInnerList.[0]._class"));
916+
}
917+
918+
@Test // DATAMONGO-1772
919+
public void mappingShouldAddTypeKeyInListOfAbstractTypeContainedInConcreteObjectCorrectly() {
920+
921+
ConcreteInner inner = new ConcreteInner();
922+
inner.abstractTypeList = Collections.<SomeAbstractType> singletonList(new SomeInterfaceImpl());
923+
List<ConcreteInner> list = Collections.singletonList(inner);
924+
925+
DBObject mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(),
926+
context.getPersistentEntity(Outer.class));
927+
928+
assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].abstractTypeList.[0]._class")
929+
.notContaining("$set.concreteInnerList.[0]._class"));
930+
}
931+
904932
static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes {
905933
ListModelWrapper concreteTypeWithListAttributeOfInterfaceType;
906934
}
@@ -1187,4 +1215,26 @@ static class SimpleValueHolder {
11871215
Integer intValue;
11881216
int primIntValue;
11891217
}
1218+
1219+
static class Outer {
1220+
List<ConcreteInner> concreteInnerList;
1221+
}
1222+
1223+
static class ConcreteInner {
1224+
List<SomeInterfaceType> interfaceTypeList;
1225+
List<SomeAbstractType> abstractTypeList;
1226+
}
1227+
1228+
interface SomeInterfaceType {
1229+
1230+
}
1231+
1232+
static abstract class SomeAbstractType {
1233+
1234+
}
1235+
1236+
static class SomeInterfaceImpl extends SomeAbstractType implements SomeInterfaceType {
1237+
1238+
}
1239+
11901240
}

0 commit comments

Comments
 (0)