问题
I have a return type which is an array of two elements. The first element is an integer and the second element is an array of dictionary with keys of sql_ident
and name
. The sql_ident
value is an integer and the name
value is a string.
I can't figure out how to model this in my response object. So it'd be like:
[
12,
[
{
"sql_ident" : 17,
"name" : "Billy Bob"
},
{
"sql_ident" : 935,
"name" : "Tina Turner"
}
]
]
回答1:
In OpenAPI/Swagger 2.0, array items must be of the same type, so there is no way to precisely model your response. The most you can do is to use a typeless schema for items
, which means the items can be anything - numbers, objects, strings, etc. - but you can't specify the exact types for items.
definitions:
MyResponse:
type: array
items: {}
In OpenAPI 3.0, multi-type arrays can be described using oneOf
and anyOf
:
components:
schemas:
MyResponse:
type: array
items:
oneOf:
- type: integer
- type: array
items:
$ref: "#/components/schemas/MyDictionary"
MyDictionary:
type: object
properties:
sql_ident:
type: string
name:
type: string
来源:https://stackoverflow.com/questions/41904148/swagger-editor-model-array-with-distinct-types