springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.26k stars 493 forks source link

@Schema allowableValues does not show in Swagger UI #2742

Closed aloussase closed 4 days ago

aloussase commented 4 days ago

Describe the bug

I am using a custom annotation to annotate a request parameter in a Spring controller.

@Schema(
        description = "Tipo de documento",
        example = "IDC",
        type = "string",
        format = "enum",
        allowableValues = {"IDC", "RUC", "PAS"}
)
public @interface DocumentType {
}

In swagger UI, this just shows as a string with no information about the allowable values.

To Reproduce Steps to reproduce the behavior:

pom.xml

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webflux-api</artifactId>
            <version>2.6.0</version>
        </dependency>

Use the annotation DocumentType on a request param in a controller and see nothing.

Expected behavior

I expect swagger UI to show either a dropdown with the values or some information about the allowable values.

Screenshots

imagen

Additional context

No answer.

bnasslahsen commented 4 days ago

@aloussase,

Not reproducible.

This is the current result:

image

Feel free to provide a Minimal, Reproducible Example - with HelloController that reproduces the problem.

This ticket will be closed, but can be reopened if your provide the reproducible sample.

aloussase commented 4 days ago

Mmmm maybe it's an issue with how I am annotating the param

@DocumentType
@RequestParam("identification_type") String identificationType

Is this the right way?

aloussase commented 4 days ago

I am using Spring Boot 3.3.3

bnasslahsen commented 4 days ago

The following just works:

@RestController
@RequestMapping
public class HelloController {

    @GetMapping("/api/testInheritance")
    public void testme (DocumentType documentType){
    }

    @Schema(
            description = "Tipo de documento",
            example = "IDC",
            type = "string",
            format = "enum",
            allowableValues = {"IDC", "RUC", "PAS"}
    )
    public @interface DocumentType {
    }
}
aloussase commented 4 days ago

But you're providing the annotation as a parameter. I have a String parameter that I need to annotate with @DocumentType. Like this:

@RestController
@RequestMapping
public class HelloController {

    @GetMapping("/api/testInheritance")
    public void testme (@DocumentType String documentType){
    }

    @Schema(
            description = "Tipo de documento",
            example = "IDC",
            type = "string",
            format = "enum",
            allowableValues = {"IDC", "RUC", "PAS"}
    )
    public @interface DocumentType {
    }
}
aloussase commented 4 days ago

Minimum reproducible example: https://github.com/aloussase/Spring.OpenApi.RequestParams.Enum.Example.

bnasslahsen commented 4 days ago

Well you didn't precise how you pass the annotation :)

You need to correct your code: And add @Retention(RetentionPolicy.RUNTIME)

    @Schema(
            description = "Tipo de documento",
            example = "IDC",
            type = "string",
            format = "enum",
            allowableValues = {"IDC", "RUC", "PAS"}
    )
    @Retention(RetentionPolicy.RUNTIME)
    public @interface DocumentType {
    }
aloussase commented 3 days ago

I see. I thought the @Inherit annotation on @Schema would take care of that. Now the example is working. But with those same changes I can't get it to work on my application. Any ideas why this might be?

aloussase commented 3 days ago

This is the whole method signature:

    @Operation(
            summary = "Listar los consumos de un cliente desde una fecha determinada",
            description = "Error en la respuesta es null",
            security = {@SecurityRequirement(name = "Bearer Authentication")}
    )
    @GetMapping("/consumptions")
    public ResponseEntity<ResponseFormatOut<List<ConsumptionDto>>> listConsumptions(
            @DocumentType
            @RequestParam("identification_type")
            String identificationType,
            @RequestParam String identification,
            @Parameter(description = "Fecha desde la cual se desea listar los consumos, en el formato YYYY-MM-DD")
            @RequestParam("date_since")
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate dateSince,
            @AuthenticationPrincipal TarjetaObsequioAuthToken token
    ) {

And the annotation:

@Schema(
        description = "Tipo de documento",
        name = "identification_type",
        example = "IDC",
        type = "string",
        format = "enum",
        allowableValues = {"IDC", "RUC", "PAS"}
)
@Retention(RetentionPolicy.RUNTIME)
public @interface DocumentType {
}
aloussase commented 3 days ago

I discovered the problem. I was using the swagger in the server instead of my local swagger. Sorry for that.