Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger/OpenAPI annotations V3 - use Enum values in swagger annotations

I'm creating the the API description of our application using Swagger/OpenApi V3 annotations, imported from following dependency:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.45</version>
</dependency>

One of the annotations is a @Schema annotation that accepts an attribute named allowableValues which allows a an array of strings:

@Schema(description = "example", 
        allowableValues = {"exampleV1", "exampleV2"}, 
        example = "exampleV1", required = true)
private String example;

Now I would like to use a custom method constructed on our Enum class that returns the allowable strings array, so it does not needs to be added upon each time we add a type to our Enum. So that we can use it like this:

public enum ExampleEnum {
    EXAMPLEV1, EXAMPLEV2;
    public static String[] getValues() {...}
}

@Schema(description = "example", 
        allowableValues = ExampleEnum.getValues(), 
        example = "exampleV1", required = true)
private String example;

Now this doesn't compile because the method is not known when executing the annotation. Is there such a solution that allows usage of Enums in the swagger V3 annotation attributes values?

Had a look in following resources:

  • https://swagger.io/docs/specification/data-models/enums/

You can define reusable enums in the global components section and reference them via $ref elsewhere.

Worst case I can indeed have it defined in one constant place and after adding a type to the Enum only have one other place needed to add the type to. But I first want to explore the above mentioned solution if it's possible.

  • https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#schema

Doesn't say anything about using any classes or dynamic generated values.

  • Enum in swagger

Is about documenting enums in swagger and not using them in the swagger annotations API.

like image 244
bramdc Avatar asked Dec 11 '19 09:12

bramdc


People also ask

What is enum in REST API?

Enums, or enumerated types, are variable types that have a limited set of possible values. Enums are popular in API design, as they are often seen as a simple way to communicate a limited set of possible values for a given property. However, enums come with some challenges that may limit or impede your API design.

What is enum variable?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.


2 Answers

try using @Schema(implementation = ExampleEnum.class, ...), you can add all other properties you want. I would need more info on your implementation but try this first.

like image 118
shapan dashore Avatar answered Oct 20 '22 15:10

shapan dashore


In my case, I have added an annotation in my enum:

@Schema(enumAsRef = true)
public enum WikipediaLanguage {
  ...
}

and then just used it annotated as a Parameter in the argument of they REST controller method:

@Parameter(
    description = "Language of the Wikipedia in use",
    required = true
) @RequestParam WikipediaLanguage lang
like image 31
Benjamín Valero Avatar answered Oct 20 '22 14:10

Benjamín Valero