如何在swagger.io中定义枚举? [英] How to define enum in swagger.io?

查看:326
本文介绍了如何在swagger.io中定义枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人能够在swaggger 2.0版的模型选项卡中定义可能的枚举值?示例: http://petstore.swagger.wordnik.com/#!/pet/ addPet 具有status属性的枚举选项,但该示例使用的是swagger 1.0版(根据JSON对象中定义的swagger版本)。我试图在版本2.0中实现相同但没有运气,不确定文档是否正确。

Does anyone was able to define possible 'enum' values in Model tab in swaggger version 2.0 ? Example here: http://petstore.swagger.wordnik.com/#!/pet/addPet has a enum option for 'status' property but that example is using version 1.0 of swagger (according to swagger version defined in JSON object). I've tried to achieve the same in version 2.0 but no luck, not sure of documentation is correct for this.

有关于此的任何提示吗?

Any hint on this?

推荐答案

enum就像这样:

      {
        "in": "query",
        "name": "sample",
        "description": "a sample parameter with an enum value",
        "type": "string",
        "enum": [ "1", "2"],
        "required": true
      }

如您所见,有一个名为 sample 的查询参数类型为string ,并有一个枚举说明两个可能的值。在这种情况下,示例声明参数是必需的,因此UI不会显示空值作为选项。

As you can see, there's a query parameter called sample of type string, and has an enum stating two possible values. In this case, the sample states the parameter is required, so the UI will not show an empty value as an option.

对于最小工作样本,请尝试:

For a minimal working sample, try this:

{
  "swagger": "2.0",
  "info": {
    "title": "title",
    "description": "descriptor",
    "version": "0.1"
  },
  "paths": {
    "/sample": {
      "post": {
        "description": "sample",
        "parameters": [
          {
            "in": "query",
            "name": "sample",
            "description": "a sample parameter with an enum value",
            "type": "string",
            "enum": [
              "1",
              "2"
            ],
            "required": true
          }
        ],
        "responses": {
          "200": {
            "description": "Successful request."
          }
        }
      }
    }
  }
}

要在本地测试它,您可以在javascript中声明一个变量(例如 spec ),并将其传递给SwaggerUi对象。 / p>

To test it locally, you can declare a variable (for example spec) in your javascript, and pass it into the SwaggerUi object.

  var spec = { ... };

  window.swaggerUi = new SwaggerUi({
    url: url,
    spec: spec,
    dom_id: "swagger-ui-container",
    supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
    onComplete: function(swaggerApi, swaggerUi){
    ...

在这种情况下, url 参数将被忽略。

The url parameter will be ignored in this case.

最终,输出如下所示:

我能够这样做,但是你可以在下面的图片上看到每个参数创建了下拉列表:

I was able to do it this way alright, but as you can see on image attached below for each params it created dropdown:

我想要实现的是很好的模型/模型模式选项卡,如下图所示,并为参数列出了可用的枚举。这是否可以在最新版本的Swagger中使用:

What I want to achieve is nice Model/Model Schema tabs as on on image below with available enums listed for parameter. Is this possible in latest version of Swagger:

这篇关于如何在swagger.io中定义枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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