Skip to content

Add support for fluent QueryResultConverter #4949

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mp911de
Copy link
Member

@mp911de mp911de commented Apr 17, 2025

We now support configuring a QueryResultConverter that has access to the raw result Document and the mapped result object that has been materialized from MongoConverter:

List<Optional<Jedi>> result = template.query(Person.class)
                        .as(Jedi.class)
                        .matching(query(where("firstname").is("luke")))
                        .map((document, reader) -> Optional.of(reader.get()))
                        .all();

This is useful to either wrap, post-process results contextually or apply further transformations based on the result Document. Changes apply for imperative and reactive find, geoNear and aggregations.

TODO:

  • QueryResultConverter.entity() (returning the converted object) is a good naming fit for queries, but with projections or aggregations we might to name it differently

@mp911de mp911de added the type: enhancement A general enhancement label Apr 17, 2025
@mp911de mp911de requested a review from Copilot April 17, 2025 10:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 21 out of 24 changed files in this pull request and generated no comments.

Files not reviewed (3)
  • pom.xml: Language not supported
  • spring-data-mongodb-distribution/pom.xml: Language not supported
  • spring-data-mongodb/pom.xml: Language not supported
Comments suppressed due to low confidence (2)

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java:3010

  • [nitpick] The unchecked cast used when comparing 'resultConverter' to QueryResultConverter.entity() could potentially lead to runtime issues. Consider refactoring this comparison to improve type safety, for example by using a more explicit identity check or redesigning the converter composition.
return resultConverter == QueryResultConverter.entity() ? (DocumentCallback<R>) readCallback : new QueryResultConverterCallback<T, R>(resultConverter, readCallback);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java:101

  • [nitpick] Resetting the result converter to QueryResultConverter.entity() in the 'as()' method may inadvertently discard any existing custom converter chain. Consider preserving or composing with the current converter to ensure intended behavior when mapping to a new return type.
return new ReactiveFindSupport<>(template, domainType, returnType, QueryResultConverter.entity(), collection, query);

@mp911de
Copy link
Member Author

mp911de commented Apr 17, 2025

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

Successfully merging this pull request may close these issues.

2 participants