Skip to content

Add support for Bean Validation's constraint groups#940

Open
nosan wants to merge 1 commit intospring-projects:mainfrom
nosan:gh-887
Open

Add support for Bean Validation's constraint groups#940
nosan wants to merge 1 commit intospring-projects:mainfrom
nosan:gh-887

Conversation

@nosan
Copy link
Copy Markdown
Contributor

@nosan nosan commented Sep 17, 2024

gh-887

  • Groups can be obtained from configuration (key: groups)
  • As an alternative solution, ConstraintDescriptions can be extended with additional methods with groups as an argument.

P.S. docs also need to be updated.

@nosan
Copy link
Copy Markdown
Contributor Author

nosan commented Mar 25, 2026

@wilkinsona
Is this something you'd be interested in supporting? I just want to understand whether I should invest time in this or not.

@wilkinsona
Copy link
Copy Markdown
Member

I have very little time for REST Docs at the moment but I shouldn't have let this sit for so long without at least acknowledging it. Sorry. I'll try to make some time to look at it properly in the next few days.

@nosan
Copy link
Copy Markdown
Contributor Author

nosan commented Mar 25, 2026

No pressure - take your time. I just wanted to check if it makes sense to proceed, since there's a merge conflict, and understand what to do with the pull request.

@wilkinsona
Copy link
Copy Markdown
Member

I've finally found some time to review this. Thanks for the PR, @nosan, and for your patience.

I consider groups to be a concept that's specific to bean validation. As such, I'd prefer to minimise how many classes know about that concept. If I were to do that building on what REST Docs offers today, I'd use a custom sub-class of ValidatorConstraintResolver. Something like this:

public class ValidatorGroupConstraintResolver extends ValidatorConstraintResolver {

	private final Class<?> group;

	public ValidatorGroupConstraintResolver(Class<?> group) {
		this.group = group;
	}

	@Override
	public List<Constraint> resolveForProperty(String property, Class<?> clazz) {
		return super.resolveForProperty(property, clazz).stream()
			.filter((constraint) -> isMember(constraint, this.group))
			.toList();
	}

	private boolean isMember(Constraint constraint, Class<?> group) {
		for (Class<?> candidate : (Class<?>[]) constraint.getConfiguration().get("groups")) {
			if (group.isAssignableFrom(candidate)) {
				return true;
			}
		}
		return false;
	}

}

I think it might be worth REST Docs making this a bit easier as, for example, dealing with the inheritance of the groups is somewhat subtle (I think I've got that right by using isAssignableFrom above). I think adding this functionality directly to ValidatorConstraintResolver makes sense. It could have a Set<Class<?>> of groups that it looks to match. When that set's null or empty it matches all constraints (current behavior). When it's not empty it matches constraints that belong to one of the specified groups.

@wilkinsona wilkinsona added the status: waiting-for-feedback Feedback is required before progress can be made label Mar 30, 2026
Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-feedback Feedback is required before progress can be made status: waiting-for-triage Untriaged issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants