Skip to content

Commit 63fbcfe

Browse files
author
Clement Petit
committed
Return saved entity reference instead of original reference in saveAll.
Make SimpleReactiveMongoRepository#saveAll(Publisher<S>) return the saved entity references instead of the original references. Closes spring-projects#3609
1 parent 5a87dec commit 63fbcfe

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
113113
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
114114

115115
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
116-
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : //
117-
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity)));
116+
mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
117+
mongoOperations.save(entity, entityInformation.getCollectionName()));
118118
}
119119

120120
/*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java

+57-5
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
import lombok.Data;
2222
import lombok.NoArgsConstructor;
23+
import lombok.Value;
24+
import lombok.With;
2325
import reactor.core.publisher.Flux;
2426
import reactor.core.publisher.Mono;
2527
import reactor.test.StepVerifier;
2628

2729
import java.util.Arrays;
2830

31+
import javax.annotation.Nullable;
32+
2933
import org.junit.Before;
3034
import org.junit.Test;
3135
import org.junit.runner.RunWith;
32-
3336
import org.springframework.beans.BeansException;
3437
import org.springframework.beans.factory.BeanClassLoaderAware;
3538
import org.springframework.beans.factory.BeanFactory;
@@ -44,7 +47,6 @@
4447
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
4548
import org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory;
4649
import org.springframework.data.mongodb.repository.support.SimpleReactiveMongoRepository;
47-
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
4850
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
4951
import org.springframework.test.context.ContextConfiguration;
5052
import org.springframework.test.context.junit4.SpringRunner;
@@ -66,9 +68,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
6668
ReactiveMongoRepositoryFactory factory;
6769
ClassLoader classLoader;
6870
BeanFactory beanFactory;
69-
ReactivePersonRepostitory repository;
71+
ReactivePersonRepository repository;
72+
ReactiveImmutablePersonRepository immutableRepository;
7073

7174
private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia;
75+
private ImmutableReactivePerson keith, james, mariah;
7276

7377
@Override
7478
public void setBeanClassLoader(ClassLoader classLoader) {
@@ -89,9 +93,11 @@ public void setUp() {
8993
factory.setBeanFactory(beanFactory);
9094
factory.setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT);
9195

92-
repository = factory.getRepository(ReactivePersonRepostitory.class);
96+
repository = factory.getRepository(ReactivePersonRepository.class);
97+
immutableRepository = factory.getRepository(ReactiveImmutablePersonRepository.class);
9398

9499
repository.deleteAll().as(StepVerifier::create).verifyComplete();
100+
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
95101

96102
dave = new ReactivePerson("Dave", "Matthews", 42);
97103
oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@@ -100,10 +106,16 @@ public void setUp() {
100106
stefan = new ReactivePerson("Stefan", "Lessard", 34);
101107
leroi = new ReactivePerson("Leroi", "Moore", 41);
102108
alicia = new ReactivePerson("Alicia", "Keys", 30);
109+
keith = new ImmutableReactivePerson(null, "Keith", "Urban", 53);
110+
james = new ImmutableReactivePerson(null, "James", "Arthur", 33);
111+
mariah = new ImmutableReactivePerson(null, "Mariah", "Carey", 51);
103112

104113
repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) //
105114
.expectNextCount(7) //
106115
.verifyComplete();
116+
immutableRepository.saveAll(Arrays.asList(keith, james, mariah)).as(StepVerifier::create) //
117+
.expectNextCount(3) //
118+
.verifyComplete();
107119
}
108120

109121
@Test // DATAMONGO-1444
@@ -325,6 +337,23 @@ public void savePublisherOfEntitiesShouldInsertEntity() {
325337
assertThat(boyd.getId()).isNotNull();
326338
}
327339

340+
@Test // GH-3609
341+
public void savePublisherOfImmutableEntitiesShouldInsertEntity() {
342+
343+
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
344+
345+
keith = keith.withId(null);
346+
james = james.withId(null);
347+
mariah = mariah.withId(null);
348+
349+
immutableRepository.saveAll(Flux.just(keith, james, mariah)).as(StepVerifier::create).expectNextCount(3)
350+
.verifyComplete();
351+
352+
assertThat(keith.getId()).isNotNull();
353+
assertThat(james.getId()).isNotNull();
354+
assertThat(mariah.getId()).isNotNull();
355+
}
356+
328357
@Test // DATAMONGO-1444
329358
public void deleteAllShouldRemoveEntities() {
330359

@@ -453,12 +482,16 @@ public void findOneByExampleWithoutResultShouldCompleteEmpty() {
453482
repository.findOne(example).as(StepVerifier::create).verifyComplete();
454483
}
455484

456-
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
485+
interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {
457486

458487
Flux<ReactivePerson> findByLastname(String lastname);
459488

460489
}
461490

491+
interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {
492+
493+
}
494+
462495
@Data
463496
@NoArgsConstructor
464497
static class ReactivePerson {
@@ -476,4 +509,23 @@ public ReactivePerson(String firstname, String lastname, int age) {
476509
this.age = age;
477510
}
478511
}
512+
513+
@With
514+
@Value
515+
static class ImmutableReactivePerson {
516+
517+
@Id String id;
518+
519+
String firstname;
520+
String lastname;
521+
int age;
522+
523+
public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) {
524+
this.id = id;
525+
this.firstname = firstname;
526+
this.lastname = lastname;
527+
this.age = age;
528+
}
529+
}
530+
479531
}

0 commit comments

Comments
 (0)