From ac181fa77f576a3dbac75525d1507fe37146f01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Petit?= Date: Fri, 26 Mar 2021 20:03:11 +0100 Subject: [PATCH] Return saved entity reference instead of original reference. Make SimpleReactiveMongoRepository#saveAll(Publisher) return the saved entity references instead of the original references. Closes #3609 --- .../SimpleReactiveMongoRepository.java | 5 +- .../SimpleReactiveMongoRepositoryTests.java | 59 +++++++++++++++++-- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index 11ba006cb3..1b32fce960 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -50,6 +50,7 @@ * @author Christoph Strobl * @author Ruben J Garcia * @author Jens Schauder + * @author Clément Petit * @since 2.0 */ public class SimpleReactiveMongoRepository implements ReactiveMongoRepository { @@ -113,8 +114,8 @@ public Flux saveAll(Publisher entityStream) { Assert.notNull(entityStream, "The given Publisher of entities must not be null!"); return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? // - mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : // - mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity))); + mongoOperations.insert(entity, entityInformation.getCollectionName()) : // + mongoOperations.save(entity, entityInformation.getCollectionName())); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java index 92b6f89739..8e352f2e8e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java @@ -20,16 +20,19 @@ import lombok.Data; import lombok.NoArgsConstructor; +import lombok.Value; +import lombok.With; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; import java.util.Arrays; +import javax.annotation.Nullable; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; @@ -44,7 +47,6 @@ import org.springframework.data.mongodb.core.ReactiveMongoTemplate; import org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory; import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository; -import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -56,6 +58,7 @@ * @author Mark Paluch * @author Christoph Strobl * @author Ruben J Garcia + * @author Clément Petit */ @RunWith(SpringRunner.class) @ContextConfiguration("classpath:reactive-infrastructure.xml") @@ -66,9 +69,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware, ReactiveMongoRepositoryFactory factory; ClassLoader classLoader; BeanFactory beanFactory; - ReactivePersonRepostitory repository; + ReactivePersonRepository repository; + ReactiveImmutablePersonRepository immutableRepository; private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia; + private ImmutableReactivePerson keith, james, mariah; @Override public void setBeanClassLoader(ClassLoader classLoader) { @@ -89,9 +94,11 @@ public void setUp() { factory.setBeanFactory(beanFactory); factory.setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT); - repository = factory.getRepository(ReactivePersonRepostitory.class); + repository = factory.getRepository(ReactivePersonRepository.class); + immutableRepository = factory.getRepository(ReactiveImmutablePersonRepository.class); repository.deleteAll().as(StepVerifier::create).verifyComplete(); + immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete(); dave = new ReactivePerson("Dave", "Matthews", 42); oliver = new ReactivePerson("Oliver August", "Matthews", 4); @@ -100,6 +107,9 @@ public void setUp() { stefan = new ReactivePerson("Stefan", "Lessard", 34); leroi = new ReactivePerson("Leroi", "Moore", 41); alicia = new ReactivePerson("Alicia", "Keys", 30); + keith = new ImmutableReactivePerson(null, "Keith", "Urban", 53); + james = new ImmutableReactivePerson(null, "James", "Arthur", 33); + mariah = new ImmutableReactivePerson(null, "Mariah", "Carey", 51); repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) // .expectNextCount(7) // @@ -325,6 +335,22 @@ public void savePublisherOfEntitiesShouldInsertEntity() { assertThat(boyd.getId()).isNotNull(); } + @Test // GH-3609 + public void savePublisherOfImmutableEntitiesShouldInsertEntity() { + + immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete(); + + immutableRepository.saveAll(Flux.just(keith, james, mariah)).as(StepVerifier::create) + .consumeNextWith(e -> keith = e) + .consumeNextWith(e -> james = e) + .consumeNextWith(e -> mariah = e) + .verifyComplete(); + + assertThat(keith.getId()).isNotNull(); + assertThat(james.getId()).isNotNull(); + assertThat(mariah.getId()).isNotNull(); + } + @Test // DATAMONGO-1444 public void deleteAllShouldRemoveEntities() { @@ -453,12 +479,16 @@ public void findOneByExampleWithoutResultShouldCompleteEmpty() { repository.findOne(example).as(StepVerifier::create).verifyComplete(); } - interface ReactivePersonRepostitory extends ReactiveMongoRepository { + interface ReactivePersonRepository extends ReactiveMongoRepository { Flux findByLastname(String lastname); } + interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository { + + } + @Data @NoArgsConstructor static class ReactivePerson { @@ -476,4 +506,23 @@ public ReactivePerson(String firstname, String lastname, int age) { this.age = age; } } + + @With + @Value + static class ImmutableReactivePerson { + + @Id String id; + + String firstname; + String lastname; + int age; + + public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) { + this.id = id; + this.firstname = firstname; + this.lastname = lastname; + this.age = age; + } + } + }