-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
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.