Skip to content

Swagger doesn't work after custom annotation replacing request parameters #2752

Closed
@Melteos

Description

@Melteos

Describe the bug

I wrapped swagger annotations in a custom annotation to get rid of code duplication. However, I lost the ability of wrapped annotations on swagger page. I should be able to define custom annotations and use them?

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using? : "3.2.0"
  • What modules and versions of springdoc-openapi are you using?: "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0"

I have a controller like this:

@RestController
public class XController {

    @GetMapping("/")
    public ResponseEntity<?> manualTranslationUpdateRun(
            @RequestParam
            @Parameter(
                examples = {
                    @ExampleObject(name = "ar-ae"),
                    @ExampleObject(name = "bg-bg"),
                    @ExampleObject(name = "cs-cz"),
                    @ExampleObject(name = "de-de"),
                    @ExampleObject(name = "el-gr"),
                    @ExampleObject(name = "en-us.src"),
                    @ExampleObject(name = "hu-hu"),
                    @ExampleObject(name = "pl-pl"),
                    @ExampleObject(name = "ro-ro"),
                    @ExampleObject(name = "sk-sk")
                }
            )
            @Size(min = 2, max = 16)
            @Pattern(regexp = "\\w+([-.]?\\w+)*")
            String locale) {
        //.....
        return ResponseEntity.accepted().build();
    }
}

Because I use the same parameter locale and their swagger configurations in other endpoints as well, it causes code duplication.
My solution was to have custom annotations for it:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Parameter(
  examples = {
    @ExampleObject(name = "ar-ae"),
    @ExampleObject(name = "bg-bg"),
    @ExampleObject(name = "cs-cz"),
    @ExampleObject(name = "de-de"),
    @ExampleObject(name = "el-gr"),
    @ExampleObject(name = "en-us.src"),
    @ExampleObject(name = "hu-hu"),
    @ExampleObject(name = "pl-pl"),
    @ExampleObject(name = "ro-ro"),
    @ExampleObject(name = "sk-sk")
  }
)
@Size(min = 2, max = 16)
@Pattern(regexp = "\\w+([-.]?\\w+)*")
public @interface LocaleParam {}

And then my controller looked like this:

@RestController
public class XController {

    @GetMapping("/")
    public ResponseEntity<?> manualTranslationUpdateRun(
                    @LocaleParam String locale) {
        //.....
        return ResponseEntity.accepted().build();
    }
}

Expected behavior

I expected to see requested param configurations previously defined explicitly to stay when moved to a wrapper annotation. I cannot see example options on swagger documentation when I run the new code.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions