Enum in swagger
Asked Answered
C

6

36

I'm wondering how to document enums in swagger.

According to JavaDoc

The dataType. See the documentation for the supported datatypes. If the data type is a custom object, set it's name, or nothing. In case of an enum use 'string' and allowableValues for the enum constants.

But I didn't find some good Java example how to really use it, specification is here.

Java

First Service

package betlista.tests.swagger;

import betlista.tests.swagger.model.Input;
import betlista.tests.swagger.model.Output;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "first", position = 1)
public class RestServiceFirst {

    @ApiOperation(value = "foo1 operation", httpMethod = "POST", position = 1, nickname = "foo")
    public void foo1(Input input) {

    }

    @ApiOperation(value = "bar1 operation", response = Output.class, httpMethod = "GET", position = 2, nickname = "bar")
    public Output bar1() {
        return null;
    }

}

Second Service

package betlista.tests.swagger;

import betlista.tests.swagger.model.Input;
import betlista.tests.swagger.model.Output;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

@Api(value = "second", position = 2)
public class RestServiceSecond {

    @ApiOperation(value = "foo2 operation", httpMethod = "POST", position = 1)
    public void foo2(Input input) {

    }

    @ApiOperation(value = "bar2 operation", response = Output.class, httpMethod = "GET", position = 2)
    public Output bar2() {
        return null;
    }

}

Input

package betlista.tests.swagger.model;

import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;

@ApiModel
public class Input {

    @ApiModelProperty(dataType = "string", allowableValues = "M, T", value = "description", notes = "notes")
    public Day day;

}

Day

package betlista.tests.swagger.model;

public enum Day {

    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;

}

Output

package betlista.tests.swagger.model;

import com.wordnik.swagger.annotations.ApiModel;

@ApiModel(value = "Output")
public class Output {

    @ApiModelProperty
    String field;

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>tests-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- generate REST documentation -->
        <dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jaxrs_2.10</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <locations>betlista.tests.swagger;betlista.tests.swagger.model</locations>
                            <apiVersion>1.0.0</apiVersion>
                            <basePath>http://localhost:port/rest</basePath>
                            <outputTemplate>${basedir}/strapdown.html.mustache</outputTemplate>
                            <outputPath>${basedir}/target/generated/strapdown.html</outputPath>
                            <swaggerDirectory>${basedir}/target/generated/apidocs</swaggerDirectory>
                            <useOutputFlatStructure>false</useOutputFlatStructure>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

You can see the result here.

There is a lot of problems in HTML output I see (missing Output description, wrong URLs, description is used for notes), but the one I do not know how to overcome is enum usage.

In tests-swagger\target\generated\apidocs\first.json should be (I think)

  "models" : {
    "Input" : {
      "id" : "Input",
      "description" : "",
      "properties" : {
        "day" : {
          "type" : "string",
          "enum" : [ "M", " T" ]
        }
      }
    }
  }

but there is

  "models" : {
    "Input" : {
      "id" : "Input",
      "description" : "",
      "properties" : {
        "day" : {
          "$ref" : "Day",
          "enum" : [ "M", " T" ]
        }
      }
    }
  }

and the $ref is a problem I think...

Any idea?

Coiffeur answered 29/5, 2014 at 14:45 Comment(1)
Refer the second post here: github.com/swagger-api/swagger-core/issues/225Malissamalissia
E
26

In case of swagger-maven-plugin 3.1.0 this might be a minimal documentation:

@ApiModel
public class Input {
    @ApiModelProperty
    public Day day;
}

@ApiModel
public enum Day {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
}

Then this is the generated json model:

"definitions" : {
  "Input" : {
    "type" : "object",
    "properties" : {
      "day" : {
        "type" : "string",
        "enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]
      }
    }
  }
}

And this is how the model is presented in the SwaggerUI:

Input {
day (string, optional) = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
}
Empery answered 17/7, 2015 at 12:45 Comment(1)
I'm using the camel swagger plugin and found annotating the enum property with: @ApiModelProperty(dataType = "string") allowed the swagger json model to then properly populate with the enum types.Royster
V
15

According to the doc you pointed:

The dataType. See the documentation for the supported datatypes. If the data type is a custom object, set it's name, or nothing. In case of an enum use 'string' and allowableValues for the enum constants.

I think you should add the enum values manually:

@ApiModel
public class Input {

    @ApiModelProperty(dataType = "string", allowableValues = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday", value = "description", notes = "notes")
    public Day day;
}
Vesicatory answered 3/9, 2014 at 4:45 Comment(1)
Sorry for a missing class, I added Day class. Yes, it is class, but how can I have the available values in documentation? I want to see somewhere in documentation that one of the values Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday is expected...Coiffeur
H
7

Custom Springfox Plugin solution:

swagger.io recommends: "If you need to specify descriptions for enum items, you can do this in the description of the parameter or property"

I implemented this recommendation based on a proprietary @ApiEnum annotation. The library is available here: https://github.com/hoereth/springfox-enum-plugin

Hatpin answered 6/11, 2018 at 8:3 Comment(0)
E
4

In OpenApi version Swagger 2.x you need to use new annotations described here: https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations

@Schema(description = "Shuttle shipment action")
public class ShuttleShipmentAction {

   @Schema(required = true, description = "Id of a shuttle shipments")
   private long id;

   @Schema(required = true, description = "Action to be performed on shuttle shipments", allowableValues = { "SEND",
        "AUTHORIZE", "REJECT", "FIX_TRAINRUN", "UNFIX_TRAINRUN", "DELETE" })
   private String action;
   ...
   ...

Resulting in something like this: enter image description here

Epiphysis answered 12/7, 2019 at 12:0 Comment(0)
E
1

You may use responseContainer with your @ApiOperation annotation:

@ApiOperation(value = "Brief description of your operation.", response = YourEnum.class, responseContainer = "List")
Endermic answered 19/4, 2017 at 14:16 Comment(0)
V
0

Thanks for this help.

I have used to this type in my code.

private String date;
@ApiModelProperty(dataType = "string", allowableValues = "FirstValue,   SecondValue", value = "description", notes = "notes")
private CarrierType carrierName;


public enum CarrierType {
    FirstValue,
    SecondValue
}

It is working fine for me.

Vivian answered 22/6, 2016 at 7:44 Comment(1)
This is a maintenance nightmare as you port the enum value as a string. Any modification to the enum, the developer has to remember to change the string in the annotation.Alphonsoalphonsus

© 2022 - 2024 — McMap. All rights reserved.