我有一个REST服务来记录, 其中一些接受简单的数组,如:
[
{ "name":"a" },
{ "name":"b" },
{ "name":"c" }
]
如何在Swagger模型部分中描述这一点?我只能像
一样创建'命名数组'model {
properties: { "arr": { "type":"array", ......
但它描述了这样的数据:
"arr": [
{ "name":"a" },
{ "name":"b" },
{ "name":"c" }
]
答案 0 :(得分:15)
/test:
post:
summary: test 123
description: test 123
parameters:
- name: param1
in: body
required: true
description: test param1
schema:
$ref: '#/definitions/stackoverflow'
responses:
200:
description: OK
这会产生:
stackoverflow2[
{
name: string
}
]
Tony的例子产生:
[
stackoverflow {
name: string
}
]
完成Swagger / OpenAPI作为YAML(复制和粘贴)
swagger: '2.0'
################################################################################
# API Information #
################################################################################
info:
version: "Two-point-Oh!"
title: Simple objects in array test
description: |
Simple objects in array test
################################################################################
# Parameters #
################################################################################
paths:
/test:
post:
summary: Array with named objects
description: Array with named objects
parameters:
- name: param1
in: body
required: true
description: test param1
schema:
type: array
items:
$ref: '#/definitions/stackoverflow'
responses:
200:
description: OK
/test2:
post:
summary: Array with simpel (nameless) objects
description: Array with simpel (nameless) objects
parameters:
- name: param1
in: body
required: true
description: test param1
schema:
$ref: '#/definitions/stackoverflow2'
responses:
200:
description: OK
definitions:
stackoverflow:
type: object
properties:
name:
type: string
description: name of the object
stackoverflow2:
type: array
items:
type: object
properties:
name:
type: string
description: name of the object
这是一个JSON版本的Swagger / OpenAPI
{
"swagger" : "2.0",
"info" : {
"description" : "Simple objects in array test\n",
"version" : "Two-point-Oh!",
"title" : "Simple objects in array test"
},
"paths" : {
"/test" : {
"post" : {
"summary" : "Array with named objects",
"description" : "Array with named objects",
"parameters" : [ {
"in" : "body",
"name" : "param1",
"description" : "test param1",
"required" : true,
"schema" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/stackoverflow"
}
}
} ],
"responses" : {
"200" : {
"description" : "OK"
}
}
}
},
"/test2" : {
"post" : {
"summary" : "Array with simpel (nameless) objects",
"description" : "Array with simpel (nameless) objects",
"parameters" : [ {
"in" : "body",
"name" : "param1",
"description" : "test param1",
"required" : true,
"schema" : {
"$ref" : "#/definitions/stackoverflow2"
}
} ],
"responses" : {
"200" : {
"description" : "OK"
}
}
}
}
},
"definitions" : {
"stackoverflow" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"description" : "name of the object"
}
}
},
"stackoverflow2" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/stackoverflow2_inner"
}
},
"stackoverflow2_inner" : {
"properties" : {
"name" : {
"type" : "string",
"description" : "name of the object"
}
}
}
}
}
答案 1 :(得分:8)
将其粘贴到http://editor.swagger.io/#/并点击“尝试此操作”
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Privacy-Service API"
},
"paths": {
"/allNames": {
"post": {
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
"parameters": [
{
"name": "request",
"in": "body",
"schema": {
"$ref": "#/definitions/ArrayOfNames"
}
}
],
"responses": {
"200": {
"description": "List of names",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
"ArrayOfNames": {
"type": "array",
"items": {
"minItems": 1,
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
}
}
}
}
答案 2 :(得分:6)
可能看起来像这样:
...
"parameters" : [{
"name" : "whatEverThatArrayCalled",
"type" : "array",
"items" : {
"$ref" : "whatEverThatArrayCalled"
}
...
}],
"models" : {
{
"id" : "whatEverThatArrayCalled",
"properties":
{
"whatEverThatArrayCalled" :
{
"type" : "array",
"items":{"name":"a",
"name":"b",
"name":"c"
},
}
}
}
}
...
答案 3 :(得分:2)
我在editor.swagger.io中尝试了以下内容,它满足了这个问题的要求并且有效。 POST请求体需要一个数组。该数组由' stackoverflow'组成。项目。每个项目都是一个具有name属性的对象。
paths:
/test:
post:
summary: test 123
description: test 123
parameters:
- name: param1
in: body
required: true
description: test param1
schema:
type: array
items:
$ref: '#/definitions/stackoverflow'
responses:
200:
description: OK
definitions:
stackoverflow:
type: object
properties:
name:
type: string
description: name of the object
答案 4 :(得分:1)
parameters:
- name: "items"
in: "formData"
description: "description"
required: "true"
type: "array"
items:
type: "object"
additionalProperties:
properties:
name:
type: "string"
根据他们的文档https://swagger.io/docs/specification/data-models/dictionaries/,这应该会产生一个数组,其对象具有名为name的属性,数据类型为字符串。
可以通过请求正文访问它,例如request.body.items
<强>更新强>
似乎这样做(没有额外的属性):
items:
type: object
properties:
name:
type: string
现在你得到的项目中每个都有一个名为name的键和一个相应的值。
如果是这样,TO要求的是什么......
答案 5 :(得分:1)
考虑你提到的阵列的格式
[
{ "name":"a" },
{ "name":"b" },
{ "name":"c" }
]
我想可以使用以下格式:
paths:
/test:
post:
description: Test request
operationId: test
parameters:
- in: body
name: requestParameter
required: true
schema:
type: array
items:
properties:
name:
type: string
responses:
'200':
description: Success response
答案 6 :(得分:-1)
如果我的理解是正确的,我认为以下可能是你想要的。
如你所说,
其中一些接受简单的数组
然后它将通过参数传递。
"parameters" : [{
"name" : "param_name",
"type" : "array",
"items" : {
"$ref" : "M"
}
...
}]
...
对于模型部分:
"models" : {
"M": {
"id" : "M",
"properties": {
"name" : {
"type" : "string"
}
}
}