swagger 怎么显示enum,如何在OpenAPI(Swagger)中定义枚举?

Does anyone know how to define possible 'enum' values in an OpenAPI 2.0 definition so that they will be displayed in the Model tab of Swagger UI? Example here: https://petstore.swagger.io/#!/pet/addPet has an enum option for the status property. How to do define such an enum in OpenAPI 2.0?

解决方案

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

3df38cc682cc2886eb2d54b1e5cc98be.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值