大摇大摆的枚举 [英] Enum in swagger

查看:27
本文介绍了大摇大摆的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 swagger 中记录枚举.

I'm wondering how to document enums in swagger.

根据 JavaDoc

数据类型.有关支持的数据类型,请参阅文档.如果数据类型是自定义对象,请设置它的名称,或者不设置任何内容.在枚举的情况下,使用 'string' 和 allowableValues 作为枚举常量.

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.

但是我没有找到一些好的Java示例如何真正使用它,规范是这里.

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

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;
    }

}

二次服务

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;
    }

}

输入

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;

}

package betlista.tests.swagger.model;

public enum Day {

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

}

输出

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>

您可以在这里查看结果.

我看到的 HTML 输出有很多问题(缺少输出描述,错误的 URL,描述用于注释),但我不知道如何克服的是枚举的使用.

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.

tests-swagger argetgeneratedapidocsfirst.json 中应该是(我认为)

In tests-swagger argetgeneratedapidocsfirst.json should be (I think)

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

但是有

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

我认为 $ref 是个问题...

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

有什么想法吗?

推荐答案

如果是 swagger-maven-plugin 3.1.0,这可能是一个最小的文档:

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;
}

那么这是生成的json模型:

Then this is the generated json model:

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

这就是模型在 SwaggerUI 中的呈现方式:

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

Input {
day (string, optional) = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
}

这篇关于大摇大摆的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆