Skip to content

Return saved entity reference instead of original reference. #3611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Christoph Strobl
* @author Ruben J Garcia
* @author Jens Schauder
* @author Clément Petit
* @since 2.0
*/
public class SimpleReactiveMongoRepository<T, ID extends Serializable> implements ReactiveMongoRepository<T, ID> {
Expand Down Expand Up @@ -113,8 +114,8 @@ public <S extends T> Flux<S> saveAll(Publisher<S> 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()));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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")
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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) //
Expand Down Expand Up @@ -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() {

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

interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
interface ReactivePersonRepository extends ReactiveMongoRepository<ReactivePerson, String> {

Flux<ReactivePerson> findByLastname(String lastname);

}

interface ReactiveImmutablePersonRepository extends ReactiveMongoRepository<ImmutableReactivePerson, String> {

}

@Data
@NoArgsConstructor
static class ReactivePerson {
Expand All @@ -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;
}
}

}