Search code examples
springcompiler-errorsmonospring-webfluxflatmap

Flatmap throws compile time error while flattened nested Mono


Have below Method

private Mono<EventSlotBook> getTestEventSlotBook(EventUserAppt eventUserAppt){
    Query query = new Query();
    query.addCriteria(
            new Criteria().andOperator(
                    Criteria.where("eventId").is(eventUserAppt.getEventId()),
                    Criteria.where("eventConfigId").is(eventUserAppt.getEventConfigId()),
                    Criteria.where("eventSlotId").is(eventUserAppt.getEventSlotId()),
                    Criteria.where("appointmentDate").in(eventUserAppt.getAppointmentDate()
                    )));

    return this.reactiveMongoTemplate.findOne(query, EventSlotBook.class)
            .flatMap(eventSlotExistingEntity -> {
                if(eventSlotExistingEntity.getEventUsers() != null) {
                    eventSlotExistingEntity.getEventUsers().add(eventUserAppt.getEventUser());
                }
                return Mono.just(eventSlotExistingEntity);
            })
            .switchIfEmpty(getInitialTestEventSlotBook(eventUserAppt));
}

And above method called by

public Mono<EventSlotBookRequestDto> saveEventSlotBookFinal(Mono<EventSlotBookRequestDto> eventSlotBookRequestDtoMono){
    log.info("Start::SaveEventSlotBook#######Final");
    Mono<EventSlotBookRequestDto> eventDtoSaved =
            eventSlotBookRequestDtoMono.map(AppUtils::dtoToEventUserApptEntity)
                    .flatMap(eventUserApptEntity -> getEventUserAppt(eventUserApptEntity))
                    .doOnNext(eventUserApptBeforeSave -> {
                        log.info("@@@@BeforeSave::{}",eventUserApptBeforeSave);
                    })
                    .flatMap(eventUserApptRepository::save)
                    .doOnNext( eventUserApptAftereSave -> {
                        log.info("@@@@AfterSave::{}",eventUserApptAftereSave);
                    })

                    .map(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) -> IDE shows it returns Mono<Mono<EventSlotBoo>>
                    .flatMap(eventSlotBookrepository::save) --> Compile time error: o instance(s) of type variable(s) exist so that Mono<EventSlotBook> conforms to EventSlotBook
                            .map(eventSlotBooEntity -> AppUtils.entityToDto((EventSlotBook)eventSlotBooEntity));



    log.info("End::SaveEventSlotBook#####Final");
    return eventDtoSaved;
}

@Repository public interface EventSlotBookRepository extends ReactiveMongoRepository<EventSlotBook,String> { }

Not sure why .flatMap(eventSlotBookrepository::save) --> Compile time error: o instance(s) of type variable(s) exist so that Mono conforms to EventSlotBook it throws this error. flatMap expected flattened Mono<Mono> to EventSlotBook and save this data


Solution

  • ReactiveMongoRepository does not have a save method which would accept a Mono. It can only accept an instance of the entity type, so the following would work:

    .flatMap(eventUserApptAfterSave -> getTestEventSlotBook(eventUserApptAfterSave)) // map changed to flatMap
    .flatMap(eventSlotBookrepository::save)