Skip to content

Commit 0ff6191

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

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
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
@@ -50,6 +50,7 @@
5050
* @author Christoph Strobl
5151
* @author Ruben J Garcia
5252
* @author Jens Schauder
53+
* @author Clément Petit
5354
* @since 2.0
5455
*/
5556
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
@@ -113,8 +114,8 @@ public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
113114
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
114115

115116
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)));
117+
mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
118+
mongoOperations.save(entity, entityInformation.getCollectionName()));
118119
}
119120

120121
/*

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

+58-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;
@@ -56,6 +58,7 @@
5658
* @author Mark Paluch
5759
* @author Christoph Strobl
5860
* @author Ruben J Garcia
61+
* @author Clément Petit
5962
*/
6063
@RunWith(SpringRunner.class)
6164
@ContextConfiguration("classpath:reactive-infrastructure.xml")
@@ -66,9 +69,11 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
6669
ReactiveMongoRepositoryFactory factory;
6770
ClassLoader classLoader;
6871
BeanFactory beanFactory;
69-
ReactivePersonRepostitory repository;
72+
ReactivePersonRepository repository;
73+
ReactiveImmutablePersonRepository immutableRepository;
7074

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

7378
@Override
7479
public void setBeanClassLoader(ClassLoader classLoader) {
@@ -89,9 +94,11 @@ public void setUp() {
8994
factory.setBeanFactory(beanFactory);
9095
factory.setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT);
9196

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

94100
repository.deleteAll().as(StepVerifier::create).verifyComplete();
101+
immutableRepository.deleteAll().as(StepVerifier::create).verifyComplete();
95102

96103
dave = new ReactivePerson("Dave", "Matthews", 42);
97104
oliver = new ReactivePerson("Oliver August", "Matthews", 4);
@@ -100,10 +107,16 @@ public void setUp() {
100107
stefan = new ReactivePerson("Stefan", "Lessard", 34);
101108
leroi = new ReactivePerson("Leroi", "Moore", 41);
102109
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);
103113

104114
repository.saveAll(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)).as(StepVerifier::create) //
105115
.expectNextCount(7) //
106116
.verifyComplete();
117+
immutableRepository.saveAll(Arrays.asList(keith, james, mariah)).as(StepVerifier::create) //
118+
.expectNextCount(3) //
119+
.verifyComplete();
107120
}
108121

109122
@Test // DATAMONGO-1444
@@ -325,6 +338,23 @@ public void savePublisherOfEntitiesShouldInsertEntity() {
325338
assertThat(boyd.getId()).isNotNull();
326339
}
327340

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

@@ -453,12 +483,16 @@ public void findOneByExampleWithoutResultShouldCompleteEmpty() {
453483
repository.findOne(example).as(StepVerifier::create).verifyComplete();
454484
}
455485

456-
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
486+
interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {
457487

458488
Flux<ReactivePerson> findByLastname(String lastname);
459489

460490
}
461491

492+
interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {
493+
494+
}
495+
462496
@Data
463497
@NoArgsConstructor
464498
static class ReactivePerson {
@@ -476,4 +510,23 @@ public ReactivePerson(String firstname, String lastname, int age) {
476510
this.age = age;
477511
}
478512
}
513+
514+
@With
515+
@Value
516+
static class ImmutableReactivePerson {
517+
518+
@Id String id;
519+
520+
String firstname;
521+
String lastname;
522+
int age;
523+
524+
public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) {
525+
this.id = id;
526+
this.firstname = firstname;
527+
this.lastname = lastname;
528+
this.age = age;
529+
}
530+
}
531+
479532
}

0 commit comments

Comments
 (0)