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:

swagger 怎么显示enum,如何在swagger.io中定义枚举?_第1张图片

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 怎么显示enum,如何在swagger.io中定义枚举?_第2张图片

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 怎么显示enum,如何在swagger.io中定义枚举?_第3张图片

你可能感兴趣的:(swagger,怎么显示enum)