Skip to content

Commit 0f97413

Browse files
petitclmp911de
authored andcommitted
Return saved entity reference instead of original reference.
Make SimpleReactiveMongoRepository#saveAll(Publisher<S>) return the saved entity references instead of the original references. Closes #3609 Original pull request: #3611.
1 parent 020da42 commit 0f97413

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Oliver Gierke
5050
* @author Christoph Strobl
5151
* @author Ruben J Garcia
52+
* @author Clément Petit
5253
* @since 2.0
5354
*/
5455
@RequiredArgsConstructor
@@ -326,8 +327,8 @@ public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
326327
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
327328

328329
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
329-
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : //
330-
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity)));
330+
mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
331+
mongoOperations.save(entity, entityInformation.getCollectionName()));
331332
}
332333

333334
/*

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

+52-4
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;
@@ -55,6 +58,7 @@
5558
* @author Mark Paluch
5659
* @author Christoph Strobl
5760
* @author Ruben J Garcia
61+
* @author Clément Petit
5862
*/
5963
@RunWith(SpringRunner.class)
6064
@ContextConfiguration("classpath:reactive-infrastructure.xml")
@@ -65,9 +69,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
6569
ReactiveMongoRepositoryFactory factory;
6670
ClassLoader classLoader;
6771
BeanFactory beanFactory;
68-
ReactivePersonRepostitory repository;
72+
ReactivePersonRepository repository;
73+
ReactiveImmutablePersonRepository immutableRepository;
6974

7075
private ReactivePerson dave, oliver, carter, boyd, stefan, leroi, alicia;
76+
private ImmutableReactivePerson keith, james, mariah;
7177

7278
@Override
7379
public void setBeanClassLoader(ClassLoader classLoader) {
@@ -88,9 +94,11 @@ public void setUp() {
8894
factory.setBeanFactory(beanFactory);
8995
factory.setEvaluationContextProvider(QueryMethodEvaluationContextProvider.DEFAULT);
9096

91-
repository = factory.getRepository(ReactivePersonRepostitory.class);
97+
repository = factory.getRepository(ReactivePersonRepository.class);
98+
immutableRepository = factory.getRepository(ReactiveImmutablePersonRepository.class);
9299

93100
repository.deleteAll().as(StepVerifier::create).verifyComplete();
101+
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
94102

95103
dave = new ReactivePerson("Dave", "Matthews", 42);
96104
oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@@ -99,6 +107,9 @@ public void setUp() {
99107
stefan = new ReactivePerson("Stefan", "Lessard", 34);
100108
leroi = new ReactivePerson("Leroi", "Moore", 41);
101109
alicia = new ReactivePerson("Alicia", "Keys", 30);
110+
keith = new ImmutableReactivePerson(null, "Keith", "Urban", 53);
111+
james = new ImmutableReactivePerson(null, "James", "Arthur", 33);
112+
mariah = new ImmutableReactivePerson(null, "Mariah", "Carey", 51);
102113

103114
repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) //
104115
.expectNextCount(7) //
@@ -324,6 +335,20 @@ public void savePublisherOfEntitiesShouldInsertEntity() {
324335
assertThat(boyd.getId()).isNotNull();
325336
}
326337

338+
@Test // GH-3609
339+
public void savePublisherOfImmutableEntitiesShouldInsertEntity() {
340+
341+
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
342+
343+
immutableRepository.saveAll(Flux.just(keith, james, mariah)).as(StepVerifier::create)
344+
.consumeNextWith(e -> keith = e).consumeNextWith(e -> james = e).consumeNextWith(e -> mariah = e)
345+
.verifyComplete();
346+
347+
assertThat(keith.getId()).isNotNull();
348+
assertThat(james.getId()).isNotNull();
349+
assertThat(mariah.getId()).isNotNull();
350+
}
351+
327352
@Test // DATAMONGO-1444
328353
public void deleteAllShouldRemoveEntities() {
329354

@@ -452,12 +477,16 @@ public void findOneByExampleWithoutResultShouldCompleteEmpty() {
452477
repository.findOne(example).as(StepVerifier::create).verifyComplete();
453478
}
454479

455-
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
480+
interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {
456481

457482
Flux<ReactivePerson> findByLastname(String lastname);
458483

459484
}
460485

486+
interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {
487+
488+
}
489+
461490
@Data
462491
@NoArgsConstructor
463492
static class ReactivePerson {
@@ -475,4 +504,23 @@ public ReactivePerson(String firstname, String lastname, int age) {
475504
this.age = age;
476505
}
477506
}
507+
508+
@With
509+
@Value
510+
static class ImmutableReactivePerson {
511+
512+
@Id String id;
513+
514+
String firstname;
515+
String lastname;
516+
int age;
517+
518+
public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) {
519+
this.id = id;
520+
this.firstname = firstname;
521+
this.lastname = lastname;
522+
this.age = age;
523+
}
524+
}
525+
478526
}

0 commit comments

Comments
 (0)