From 7cf69c5b1a2cbffcd0d687bf3de3b8b801cb0f90 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 4 Sep 2017 15:35:36 +0200 Subject: [PATCH] DATAMONGO-1772 - Fix UpdateMapper type key rendering for abstract list elements contained in concrete typed ones. Original pull request: #497. --- .../mongodb/core/convert/UpdateMapper.java | 7 ++- .../core/convert/UpdateMapperUnitTests.java | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java index f2d6be356..6efcac5e5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/UpdateMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package org.springframework.data.mongodb.core.convert; +import java.util.Collection; import java.util.Map.Entry; import org.bson.Document; @@ -226,6 +227,10 @@ public class UpdateMapper extends QueryMapper { return info; } + if (source instanceof Collection) { + return NESTED_DOCUMENT; + } + if (!type.equals(source.getClass())) { return info; } 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 9af4a9933..833ef568d 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 @@ -932,6 +932,34 @@ public class UpdateMapperUnitTests { } } + @Test // DATAMONGO-1772 + public void mappingShouldAddTypeKeyInListOfInterfaceTypeContainedInConcreteObjectCorrectly() { + + ConcreteInner inner = new ConcreteInner(); + inner.interfaceTypeList = Collections.singletonList(new SomeInterfaceImpl()); + List list = Collections.singletonList(inner); + + Document mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(), + context.getPersistentEntity(Outer.class)); + + assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].interfaceTypeList.[0]._class") + .notContaining("$set.concreteInnerList.[0]._class")); + } + + @Test // DATAMONGO-1772 + public void mappingShouldAddTypeKeyInListOfAbstractTypeContainedInConcreteObjectCorrectly() { + + ConcreteInner inner = new ConcreteInner(); + inner.abstractTypeList = Collections.singletonList(new SomeInterfaceImpl()); + List list = Collections.singletonList(inner); + + Document mappedUpdate = mapper.getMappedObject(new Update().set("concreteInnerList", list).getUpdateObject(), + context.getPersistentEntity(Outer.class)); + + assertThat(mappedUpdate, isBsonObject().containing("$set.concreteInnerList.[0].abstractTypeList.[0]._class") + .notContaining("$set.concreteInnerList.[0]._class")); + } + static class DomainTypeWrappingConcreteyTypeHavingListOfInterfaceTypeAttributes { ListModelWrapper concreteTypeWithListAttributeOfInterfaceType; } @@ -1218,4 +1246,26 @@ public class UpdateMapperUnitTests { Integer intValue; int primIntValue; } + + static class Outer { + List concreteInnerList; + } + + static class ConcreteInner { + List interfaceTypeList; + List abstractTypeList; + } + + interface SomeInterfaceType { + + } + + static abstract class SomeAbstractType { + + } + + static class SomeInterfaceImpl extends SomeAbstractType implements SomeInterfaceType { + + } + }