Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

ReactiveMongoOperationsSessionRepository deleteById throws java.lang.NullPointerException when session is not found #97

@cmario

Description

@cmario

Hi,

I am using spring-session-data-mongodb 2.1.3.RELEASE with spring boot 2.1.5.RELEASE and jdk 11 and I am facing an issue if I have a valid session cookie and for any reason the related session is missing in MongoDB. The deleteById method throws the exception below:

java.lang.NullPointerException: null
	at org.springframework.session.events.AbstractSessionEvent.<init>(AbstractSessionEvent.java:40) ~[spring-session-core-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.session.events.SessionDestroyedEvent.<init>(SessionDestroyedEvent.java:36) ~[spring-session-core-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.session.events.SessionDeletedEvent.<init>(SessionDeletedEvent.java:39) ~[spring-session-core-2.1.6.RELEASE.jar:2.1.6.RELEASE]
	at org.springframework.session.data.mongo.ReactiveMongoOperationsSessionRepository.lambda$deleteById$6(ReactiveMongoOperationsSessionRepository.java:142) ~[spring-session-data-mongodb-2.1.3.RELEASE.jar:na]

Looking at the code I think there is a mistake in using "doOnSuccess":

/**
 * Deletes the {@link MongoSession} with the given {@link MongoSession#getId()} or does nothing if the
 * {@link MongoSession} is not found.
 *
 * @param id the {@link MongoSession#getId()} to delete
 */
@Override
public Mono<Void> deleteById(String id) {

	return findSession(id)
			.flatMap(document -> this.mongoOperations.remove(document, this.collectionName).then(Mono.just(document)))
			.map(document -> convertToSession(this.mongoSessionConverter, document))
			.doOnSuccess(mongoSession -> publishEvent(new SessionDeletedEvent(this, mongoSession))) //
			.then();
}

"doOnSuccess" is called even if findSession completes without data, that means mongoSession is null. "doOnNext" should be used instead.

Regards
Mario

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions