Skip to content

Support meta-annotations for org.springframework.web request annotations #30761

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

Closed
untra opened this issue Jun 27, 2023 · 1 comment
Closed
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@untra
Copy link

untra commented Jun 27, 2023

Affects: <Spring Framework version>


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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 27, 2023
@me0106
Copy link

me0106 commented Sep 28, 2023

This may be completed soon, or it may take a long time. 🤣
duplicate #21829

@sbrannen sbrannen added status: duplicate A duplicate of another issue in: web Issues in web modules (web, webmvc, webflux, websocket) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 28, 2023
@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants