Skip to content

Validation Meta annotations not working #4886

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

Open
RafTacker opened this issue May 8, 2025 · 0 comments
Open

Validation Meta annotations not working #4886

RafTacker opened this issue May 8, 2025 · 0 comments

Comments

@RafTacker
Copy link

RafTacker commented May 8, 2025

We are using the following to define our own custom validation annotations in a spring boot project.

Validation annotation

@Min(0)
@Max(999)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {})
public @interface ValidStoreId {

	String message() default "Invalid store ID";

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

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

}

Controller declaration

@RestController
@Validated
@RequestMapping("/api/teststores/v1")
@RequiredArgsConstructor
public class TestStoreController {

	@GetMapping(path = "/{storeId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public TestStoreDto getStore(@PathVariable @NotNull @ValidStoreId final Short storeId) {
		return new TestStoreDto();
	}
}

DTO

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TestStoreDto {

   @Min(0)
   @Max(999)
   @NotNull
   private Short storeId;

   @ValidStoreId
   @NotNull
   private Short metaStoreId;

}

The validation annotation is getting processed correctly throughout jakarta validation processing.
If the meta annotation is used on a path variable in a Controller, it is correctly processed.

"/api/teststores/v1/{storeId}": {
	"get": {
		"tags": [
			"test-store-controller"
		],
		"operationId": "getStore",
		"parameters": [
			{
				"name": "storeId",
				"in": "path",
				"required": true,
				"schema": {
					"type": "integer",
					"format": "int32",
					"maximum": 999,
					"minimum": 0
				}
			}
		]
	}
}

The apidocs generated with this custom annotation, do not contain the limits, if it is used in a DTO (see metaStoreId in the example below).
Of course annotating the Data class directly with @min / @max produces the correct output (see storeId in the example below).

            "TestStoreDto": {
                "type": "object",
                "properties": {
                    "storeId": {
                        "type": "integer",
                        "format": "int32",
                        "maximum": 999,
                        "minimum": 0
                    },
                    "metaStoreId": {
                        "type": "integer",
                        "format": "int32"
                    }
                },
                "required": [
                    "metaStoreId",
                    "storeId"
                ]
            }

We feel like this is a bug in the schema generation, as generally meta annotations are processed correctly (as proven by the correctly generated ouput for the PathVariable).
Processing of meta annotations seems to be just missing within DTO classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant