Affects: \


Hi there 👋 I'm a long time enjoyer of spring boot.

We use Spring Boot with springdoc, javax.validation and swagger-parser to manage our spring controllers, input validation and generate our OpenAPI specification with great success. However using all of these tools has led to an accumulation of annotations on our controllers, their methods, and their parameters. This is becoming fragile with copypasta.

Use Case

We want to unify our pagination across our controllers. Currently routes that paginate will accept the following params:

fun listWhatever(
      @Valid
      @Parameter(description = Utils.PAGINATION_PAGESIZE_PARAM_DESCRIPTION)
      @Schema(minimum = "0", required = false, defaultValue = "10")
      @RequestParam(Utils.PAGINATION_PAGESIZE_PARAM, required = false, defaultValue = "10")
      pageSize: Number,
      @Valid
      @Parameter(description = Utils.PAGINATION_PAGETOKEN_PARAM_DESCRIPTION)
      @Schema(minimum = "0", required = false, defaultValue = "0")
      @RequestParam(Utils.PAGINATION_PAGETOKEN_PARAM, required = false, defaultValue = "0")
      pageToken: Number,
      @Valid
      @Parameter(description = Utils.PAGINATION_SORTFIELD_PARAM_DESCRIPTION)
      @Schema(allowableValues = ["name", "id", "createdDate"], required = false, defaultValue = "name")
      @RequestParam(Utils.PAGINATION_SORTFIELD_PARAM, required = false, defaultValue = "name")
      sortField: String,
      @Valid
      @Parameter(description = Utils.PAGINATION_SORTDIR_PARAM_DESCRIPTION)
      @Schema(allowableValues = ["asc", "desc"], required = false, defaultValue = "desc")
      @RequestParam(Utils.PAGINATION_SORTDIR_PARAM, required = false, defaultValue = "desc")
      sortDir: String = "desc",
  )

We have the same code-segments in many methods and controllers. While this is very powerful for us with our spring controller wiring and openapi spec generation, we hope to reduce the copy-pasta and enhance consistency with meta-annotations that can apply all of these common annotation patterns for us:

fun listWhatever(
      @Valid @PageSizePagination
      pageSize: Number,
      @Valid @PageTokenPAgination
      pageToken: Number,
      @Valid @SortFieldPagination
      sortField: String,
      @Valid @SortDirPagination
      sortDir: String = "desc",
  )

this could be possible with an annotation class, however:

@Target(AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
@Inherited
@Parameter(description = Utils.PAGINATION_SORTDIR_PARAM_DESCRIPTION)
@Schema(allowableValues = ["asc", "desc"], required = false, defaultValue = "desc")
@RequestParam(Utils.PAGINATION_SORTDIR_PARAM, required = false, defaultValue = "desc") // <--- This annotation is not applicable to target 'annotation class'
annotation class SortDirPagination

the @RequestParam annotation only targets parameters, and so This annotation is not applicable to target 'annotation class'.

Possible Solution

If spring framework request annotations could also support targeting ANNOTATION_TYPE, they could be used in kotlin annotation classes, and we would have a solution to unify our spring pagination controls nicely.

As a poweruser of spring boot web annotations, I want the following annotations updated with support for ANNOTATION_TYPE targeting, so I can use them with other parameter annotations: @PathVariable @RequestParam @RequestMapping @RequestBody @RequestHeader

Comment From: me0106

This may be completed soon, or it may take a long time. 🤣 duplicate https://github.com/spring-projects/spring-framework/issues/21829