gpt4 book ai didi

swagger - 如何在 Swagger 中描述具有简单对象的数组的模型?

转载 作者:行者123 更新时间:2023-12-03 06:19:31 25 4
gpt4 key购买 nike

我有一个 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"
}
}
}
}
}

关于swagger - 如何在 Swagger 中描述具有简单对象的数组的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19585581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com