Skip to content

fix: resolve Validation Meta annotations not working (#4886)#4986

Merged
daniel-kmiecik merged 4 commits into
swagger-api:masterfrom
kuntal1461:bug/4886-validation-meta-annotations
May 12, 2026
Merged

fix: resolve Validation Meta annotations not working (#4886)#4986
daniel-kmiecik merged 4 commits into
swagger-api:masterfrom
kuntal1461:bug/4886-validation-meta-annotations

Conversation

@kuntal1461
Copy link
Copy Markdown
Contributor


Pull Request

Thank you for contributing to swagger-core!

Please fill out the following information to help us review your PR efficiently.


Description

This PR fixes an issue where Validation Meta annotations were not being correctly recognized by the ModelResolver.

Problem

When developers use composed validation annotations (for example, a custom @MyNotBlank built on top of @NotBlank), swagger-core failed to introspect these Meta annotations, causing validation metadata to be lost in generated models.

Solution

Enhanced ModelResolver to introspect and apply Meta annotations properly.
A new test class ComposedConstraintMetaAnnotationTest verifies correct handling of composed constraint annotations.

Fixes: #4886


Type of Change

  • 🐛 Bug fix
  • 🧪 Tests

Checklist


Screenshots / Additional Context

  • Verified that composed constraint annotations are now detected and applied.

  • No regressions in direct annotation handling.

  • Affected files:

    • modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java
    • modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/ComposedConstraintMetaAnnotationTest.java

@ewaostrowska ewaostrowska force-pushed the bug/4886-validation-meta-annotations branch from def7aee to 9268835 Compare October 29, 2025 07:19
@ewaostrowska
Copy link
Copy Markdown
Contributor

Hi @kuntal1461,
Thanks a lot for providing the PR, it is a step in the right direction to resolve Validation Meta annotations issue.

To be able to merge these changes we need some adjustments:

  • [Bug]: SpecFilter skips the ApiResponse#$ref field #4960 fix should be removed from this PR
  • resolving Validation Meta annotations does currently work on a path variable in a Controller which means that at least part of the code could be reused in the case of DTOs as well. The logic for controllers meta annotations is in ParameterProcessor and it needs to be adjusted before applying it in ModelResolver. Maybe consider moving it to a new class
  • the logic currently recognizes only javax.validation.*. jakarta.validation.* should be recognized as well
  • Test:
    - Add the new test using jakarta.validation.constraints.*
    - Test composed constraints for @SiZe, @pattern etc
    - Add a parameter vs. DTO parity test to prevent regressions

@Mattias-Sehlstedt
Copy link
Copy Markdown
Contributor

Could we add a test that mimics how @Range works? I believe the current implementation might interpret such a structure oddly, since it will parse the "unused" meta-parameters since it does not handle the override functionality.

See class definition below, and the functionality offered by @OverridesAttribute.

@Documented
@Constraint(validatedBy = { })
@SupportedValidationTarget(ValidationTarget.ANNOTATED_ELEMENT)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Repeatable(List.class)
@Min(0)
@Max(Long.MAX_VALUE)
@ReportAsSingleViolation
public @interface Range {
	@OverridesAttribute(constraint = Min.class, name = "value") long min() default 0;

	@OverridesAttribute(constraint = Max.class, name = "value") long max() default Long.MAX_VALUE;

	String message() default "{org.hibernate.validator.constraints.Range.message}";

	Class<?>[] groups() default { };

	Class<? extends Payload>[] payload() default { };

	/**
	 * Defines several {@code @Range} annotations on the same element.
	 */
	@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
	@Retention(RUNTIME)
	@Documented
	public @interface List {
		Range[] value();
	}
}

Issue basically what is mentioned in issue.

@daniel-kmiecik daniel-kmiecik force-pushed the bug/4886-validation-meta-annotations branch from e684506 to bcfc7bb Compare May 11, 2026 08:14
@daniel-kmiecik daniel-kmiecik force-pushed the bug/4886-validation-meta-annotations branch from bcfc7bb to cb540da Compare May 11, 2026 08:26
@daniel-kmiecik
Copy link
Copy Markdown
Collaborator

@kuntal1461 thanks for your contribution and for preparing the fix.
Great work on refining the solution and addressing the issue.
Really appreciate the time and effort you put into it!

@daniel-kmiecik daniel-kmiecik merged commit 7ae37cb into swagger-api:master May 12, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation Meta annotations not working

5 participants