Skip to content

Javadoc for ModelAttributeMethodProcessor.validateIfApplicable is misleading [SPR-12655] #17256

Closed
@spring-projects-issues

Description

@spring-projects-issues

Lukas Hinsch opened SPR-12655 and commented

Update:
The javadoc for ModelAttributeMethodProcessor.validateIfApplicable claims that validation is triggered by checking for annotation of type javax.validation.Valid, whereas the actual implementation looks for annotation type names starting with "Valid" to allow for additional annotations (see comment below).
To avoid confusion the javadoc should be adjusted to reflect this behavior (such as "The default implementation checks for annotations starting with "Valid" to cover {@code javax.validation.Valid}, {@code
org.springframework.validation.annotation.Validated} and others.")

Was originally:
When checking for annotation javax.validation.Valid the code only checks if the simple class name starts with "Valid".
I was playing around with controller aspects in combination with custom annotations that by accident also started with "Valid" and it took me a while to understand why bean validation was triggered by my custom annotation.

So I think in: ModelAttributeMethodProcessor:162
the code should be changed from

if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid"))

to something like

if (validatedAnn != null || ann.annotationType().getName().equals("javax.validation.Valid"))

(https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/method/annotation/ModelAttributeMethodProcessor.java#L162)


Affects: 3.2.13, 4.1.4

Referenced from: commits 9a7871f, 7585be8, d77af71, 186fef6, 25644db, 7191050

Backported to: 3.2.14

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions