Skip to content

Commit ccb9f11

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 f64b177 commit ccb9f11

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-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
@@ -47,6 +47,7 @@
4747
* @author Oliver Gierke
4848
* @author Christoph Strobl
4949
* @author Ruben J Garcia
50+
* @author Clément Petit
5051
* @since 2.0
5152
*/
5253
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
@@ -333,8 +334,8 @@ public <S extends T> Flux<S> saveAll(Publisher<S> entityStream) {
333334
Assert.notNull(entityStream, "The given Publisher of entities must not be null!");
334335

335336
return Flux.from(entityStream).flatMap(entity -> entityInformation.isNew(entity) ? //
336-
mongoOperations.insert(entity, entityInformation.getCollectionName()).then(Mono.just(entity)) : //
337-
mongoOperations.save(entity, entityInformation.getCollectionName()).then(Mono.just(entity)));
337+
mongoOperations.insert(entity, entityInformation.getCollectionName()) : //
338+
mongoOperations.save(entity, entityInformation.getCollectionName()));
338339
}
339340

340341
/*

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

+54-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,6 +107,9 @@ 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) //
@@ -325,6 +335,22 @@ public void savePublisherOfEntitiesShouldInsertEntity() {
325335
assertThat(boyd.getId()).isNotNull();
326336
}
327337

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)
345+
.consumeNextWith(e -> james = e)
346+
.consumeNextWith(e -> mariah = e)
347+
.verifyComplete();
348+
349+
assertThat(keith.getId()).isNotNull();
350+
assertThat(james.getId()).isNotNull();
351+
assertThat(mariah.getId()).isNotNull();
352+
}
353+
328354
@Test // DATAMONGO-1444
329355
public void deleteAllShouldRemoveEntities() {
330356

@@ -453,12 +479,16 @@ public void findOneByExampleWithoutResultShouldCompleteEmpty() {
453479
repository.findOne(example).as(StepVerifier::create).verifyComplete();
454480
}
455481

456-
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
482+
interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {
457483

458484
Flux<ReactivePerson> findByLastname(String lastname);
459485

460486
}
461487

488+
interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {
489+
490+
}
491+
462492
@Data
463493
@NoArgsConstructor
464494
static class ReactivePerson {
@@ -476,4 +506,23 @@ public ReactivePerson(String firstname, String lastname, int age) {
476506
this.age = age;
477507
}
478508
}
509+
510+
@With
511+
@Value
512+
static class ImmutableReactivePerson {
513+
514+
@Id String id;
515+
516+
String firstname;
517+
String lastname;
518+
int age;
519+
520+
public ImmutableReactivePerson(@Nullable String id, String firstname, String lastname, int age) {
521+
this.id = id;
522+
this.firstname = firstname;
523+
this.lastname = lastname;
524+
this.age = age;
525+
}
526+
}
527+
479528
}

0 commit comments

Comments
 (0)