Skip to content

Inconsistent nullability of read() in templates: not sure if mapper can skip a document #3648

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
pkubowicz opened this issue May 13, 2021 · 3 comments
Assignees
Labels
type: task A general task

Comments

@pkubowicz
Copy link

pkubowicz commented May 13, 2021

In spring-data-mongodb 3.1.8 ReactiveMongoTemplate.ReadDocumentCallback#doWith includes code to deal with null:

			T source = reader.read(type, document);
			if (source != null) {
                        // …
			}

			return Mono.empty();

This code suggests that I can write a custom reader that returns null for elements that cannot be mapped and should be skipped.

However, this is not true. org.springframework.data.convert.EntityReader is not annotated with @Nullable, so source can never be null. In Kotlin, the compiler won't allow writing an implementation returning null.

Use case

By default it is impossible to use findAll on a collection that contains one malformed document: mapping exception will crash the operation and no elements will be emitted. I am looking for a way to ignore invalid documents and return remaining ones.

If I am mistaken and there is a way to write a custom mapper that is able to skip documents that cannot be mapped, please let me know.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 13, 2021
@pkubowicz pkubowicz changed the title Inconsistent nullability of read() in templates Inconsistent nullability of read() in templates: not sure if mapper can skip a document May 14, 2021
@mp911de
Copy link
Member

mp911de commented May 14, 2021

While EntityReader should never return null, there are possibilities where a Converter might return null as Spring's Converter may return null.

In Kotlin, the compiler won't allow writing an implementation returning null.

Actually, Kotlin suggests the right thing to do.

I am looking for a way to ignore invalid documents and return remaining ones.

Spring Data actually never intended to support skipping results, it happened more by accident. I'd suggest registering a lifecycle listener that allows you to update the document prior to mapping it.

@pkubowicz
Copy link
Author

Thanks for the explanation. Do you plan to remove the null-handling branch from the code then?

@mp911de mp911de self-assigned this May 17, 2021
@mp911de mp911de added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged labels May 18, 2021
@mp911de mp911de added this to the 3.3 M1 (2021.1.0) milestone May 18, 2021
@mp911de
Copy link
Member

mp911de commented May 18, 2021

We decided to revisit the mentioned code paths and to remove code paths that allow for null as a result of EntityReader. Thanks for making us aware of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

No branches or pull requests

3 participants