swagger 怎么显示enum_如何在swagger.io中定义枚举?

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" works just like this:

{

"in": "query",

"name": "sample",

"description": "a sample parameter with an enum value",

"type": "string",

"enum": [ "1", "2"],

"required": true

}

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."

}

}

}

}

}

}

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){

...

The url parameter will be ignored in this case.

Eventually, the output looks like this:

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

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值