我有一个 REST 服务要记录, 其中一些接受简单的数组,例如:
[
{ "name":"a" },
{ "name":"b" },
{ "name":"c" }
]
如何在 Swagger 模型部分描述这一点?我只能创建“命名数组”,例如
model {
properties: { "arr": { "type":"array", ......
但它描述的数据是这样的:
"arr": [
{ "name":"a" },
{ "name":"b" },
{ "name":"c" }
]
Tony YUEN 很接近,但没有雪茄。这是在 OpenAPI/Swagger 2.0 中使用 YAML 的正确定义:
/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
}
]
托尼的例子产生:
[
stackoverflow {
name: string
}
]
以 YAML 形式完成 Swagger/OpenAPI(复制和粘贴):
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
这是 Swagger/OpenAPI 2.0 的 JSON 版本:
{
"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"
}
}
}
}
}
将此粘贴到 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"
}
}
}
}
}
}
考虑您提到的数组的格式
[
{ "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
它可能看起来像这样:
...
"parameters" : [{
"name" : "whatEverThatArrayCalled",
"type" : "array",
"items" : {
"$ref" : "whatEverThatArrayCalled"
}
...
}],
"models" : {
{
"id" : "whatEverThatArrayCalled",
"properties":
{
"whatEverThatArrayCalled" :
{
"type" : "array",
"items":{"name":"a",
"name":"b",
"name":"c"
},
}
}
}
}
...
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 要求的是什么......
我在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
如果我的理解是正确的,我认为以下可能是您想要的。
正如你提到的,
其中一些接受简单数组
然后它会通过一个参数传递。
"parameters" : [{
"name" : "param_name",
"type" : "array",
"items" : {
"$ref" : "M"
}
...
}]
...
对于模型部分:
"models" : {
"M": {
"id" : "M",
"properties": {
"name" : {
"type" : "string"
}
}
}