有人知道如何在 OpenAPI 2.0定义中定义可能的 enum值,以便将它们显示在 Swagger UI 的 Model 选项卡中吗?< br/> 举个例子对于 status属性有一个枚举选项。< br/> 如何在 OpenAPI 2.0中定义这样的枚举?
enum
status
“ enum”在 OpenAPI 2.0中是这样工作的:
{ "in": "query", "name": "sample", "description": "a sample parameter with an enum value", "type": "string", "enum": [ "1", "2"], "required": true }
以及 OpenAPI 3.0:
{ "in": "query", "name": "sample", "description": "a sample parameter with an enum value", "schema": { "type": "string", "enum": [ "1", "2"] }, "required": true }
如您所见,有一个名为 sample的字符串类型的查询参数,并且有一个枚举来表示两个可能的值。在这种情况下,示例声明参数是必需的,因此 UI 将不会显示空值作为选项。
sample
对于最小的工作样本,请尝试以下方法:
{ "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 对象。
spec
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参数。
url
最终,输出结果如下:
这应该会奏效:
{ "name": "bookingType", "in": "path", "type": "array", "items": { "enum": [ "packages", "accommodations" ] }, "description": "lorem ipsum" }
参考资料 https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#itemsObject
这不是确切的答案,但它可能为您工作,直到他们添加这个功能。
像这样简单地声明属性
"MyObject":{ "properties":{ "MyEnum":{ "type":"Value1 or Value2 or Value3" } } }
您的 ModelSchema 将显示 {},但是 Model 将显示 MyEnum(Value1 or Value2 or Value3, optional)
{}
MyEnum(Value1 or Value2 or Value3, optional)
使用 YAML 语法更新它。
OpenAPI 2.0:
parameters: - in: query name: sample description: a sample parameter with an enum value type: string enum: - 1 - 2 required: true
OpenAPI 3.0:
parameters: - in: query name: sample description: a sample parameter with an enum value schema: type: string enum: - 1 - 2 required: true