Skip to content
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

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

Closed
Melteos opened this issue Oct 8, 2024 · 2 comments
Closed
Labels
enhancement New feature or request

Comments

@Melteos
Copy link

Melteos commented Oct 8, 2024

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

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Oct 8, 2024

@Melteos,

After adjusting your custom annotaiton:

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Parameter(
		schema = @Schema(type = "string", maxLength = 16, minLength = 2, pattern = "\\w+([-.]?\\w+)*"),
		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+)*")
@interface LocaleParam {}

This is the resulting spec, :

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /:
    get:
      tags:
        - x-controller
      operationId: manualTranslationUpdateRun
      parameters:
        - name: locale
          in: query
          required: true
          schema:
            maxLength: 16
            minLength: 2
            pattern: \w+([-.]?\w+)*
            type: string
          examples:
            el-gr:
              description: el-gr
            pl-pl:
              description: pl-pl
            de-de:
              description: de-de
            hu-hu:
              description: hu-hu
            en-us.src:
              description: en-us.src
            ro-ro:
              description: ro-ro
            sk-sk:
              description: sk-sk
            bg-bg:
              description: bg-bg
            ar-ae:
              description: ar-ae
            cs-cz:
              description: cs-cz
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: object
components: {}

Examples are showing in the OpenAPI spec.
I have added a fix, to handle the custom annotation with JSR 303 meta-annotations, out of the box.
If you are expecting any other spec, feel free to add it, and this ticket can be reopened.

@Melteos
Copy link
Author

Melteos commented Oct 9, 2024

that worked, thank you 🙏

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

No branches or pull requests

2 participants