Skip to content
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

Allow query projection methods .include() and .exclude() to accept Collection #4625

Open
takanuva15 opened this issue Jan 26, 2024 · 0 comments · May be fixed by #4668
Open

Allow query projection methods .include() and .exclude() to accept Collection #4625

takanuva15 opened this issue Jan 26, 2024 · 0 comments · May be fixed by #4668
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement

Comments

@takanuva15
Copy link

Hi, we have a bunch of APIs that allow our users to specify a comma-separated list of fields that they want to receive back from the API response, which lowers our network bandwidth usage:

@GetMapping("/books")
public List<Book> getAllBooks(
	@RequestParam(defaultValue = "") Set<String> fieldsToInclude     //Set removes duplicate fields
) {
	return bookDAO.getBooks(fieldsToInclude)
}

Currently, spring-data allows us to project fields on a query using the include() and exclude() methods. However, the include() method only accepts String or String.... Thus, we're forced to add code to convert our Lists into String arrays just to satisfy the include() function's signature.

public List<Book> getBooks(Collection<String> fieldsToInclude) {
    var query = new Query();
    query.fields().include(fieldsToInclude.toArray(new String[0]));
    query.with(Sort.by("date").descending());
    return mongoTemplate.find(query, Book.class);
}

This adds complexity and maintenance overhead even though Collection is the more flexible and more common data structure to use in Java code over String[].

So, I would like to propose adding a new version of include and exclude that accepts Collection so that we can directly use modern Collection implementors (List, Set, etc) for these methods instead of being forced to convert them to primitive String[] first?


The implementation would be pretty simple, so this should be an easy upgrade that would greatly improve the public API of spring-data-mongo Query objects to make them more flexible and usable:

public Field include(Collection<String> fields) {
	Assert.notNull(fields, "Keys must not be null!");
	for (String key : fields) {
		criteria.put(key, 1);
	}
	return this;
}
// similar for exclude

(I can raise a PR for this if that's easier for the spring team)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 26, 2024
@christophstrobl christophstrobl added type: enhancement A general enhancement status: ideal-for-contribution An issue that a contributor can help us with and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Projects
None yet
3 participants