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

Collection/varargs utility methods for composing Specifications [DATAJPA-1651] #1943

Closed
spring-projects-issues opened this issue Dec 27, 2019 · 1 comment
Assignees
Labels
type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Daniel Shuy Yaw Yong opened DATAJPA-1651 and commented

Currently, when handling multiple conditional Specifications, the code looks something like:

Specification<User> specification = Specification.where(null);
if (nameFilter != null && !nameFilter.isEmpty()) {
  specification = specification.and(specifications.hasName(nameFilter));
}
if (typeFilter != null) {
  specification = specification.and(specifications.hasType(typeFilter));
}
if (archivedFilter != null) {
  specification = specification.and(specifications.isArchived(archived));
}
if (searchPattern != null && !searchPattern.isEmpty()) {
  specification = specification.and(
    specifications.nameLike(searchPattern).or(
      specifications.addressLike(searchPattern)));
}

return repository.findAll(specification);

This approach has the following disadvantages:

  • It is quite verbose
  • Some IDEs will emit potential NullPointerException warnings on the Specification#and(Specification) calls (because Specification#where(Specification) is @Nullable)
  • It forces us to mutate the specification variable
  • Can get very messy when nesting multiple AND/OR operations

 

This is a feature request to add static utility methods to create conjunctions/disjuntions of Collections/varargs of Specifications, similar to Querydsl's ExpressionUtils#allOf(Collection) / ExpressionUtils#allOf(Predicate...) / ExpressionUtils#anyOf(Collection) / ExpressionUtils#anyOf(Predicate...)

 

This will allow for better structured code when handling multiple conditional Specifications, eg. using the same example above:

var specification = Specification.allOf(
    nameFilter != null && !nameFilter.isEmpty() ? specifications.hasName(nameFilter) : null,
    typeFilter != null ? specifications.hasType(typeFilter) : null,
    archivedFilter != null ? specifications.isArchived(archivedFilter) : null),
    searchPattern != null && !searchPattern.isEmpty() ? Specification.anyOf(
        specifications.nameLike(searchPattern),
        specifications.addressLike(searchPattern));

return repository.findAll(specification);

 

 


Issue Links:

  • DATAJPA-1622 Nullable annotations used for non-null returning Specification methods

Referenced from: pull request #404

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

This is going to be part of a major revamp of our Specification story for the release train post Neumann. Rest assured we will get to that but it will very likely to be post Neumann release, currently scheduled for May

@spring-projects-issues spring-projects-issues added the type: enhancement A general enhancement label Dec 30, 2020
@gregturn gregturn self-assigned this May 5, 2022
gregturn pushed a commit that referenced this issue May 5, 2022
gregturn added a commit that referenced this issue May 5, 2022
@gregturn gregturn closed this as completed May 5, 2022
gregturn added a commit that referenced this issue May 5, 2022
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

No branches or pull requests

3 participants