[英]How to document multiple content types in successful GET response in swagger
假設我們有一個示例 json swagger 規范:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Some API"
},
"basePath": "/api/v1",
"consumes": [
"application/json"
],
"produces": [
"application/json",
"text/csv"
],
"paths": {
"/some/endpoint": {
"get": {
"parameters": [
{
"in": "body",
"name": "body",
"required": false,
"schema": {
"$ref": "#/definitions/BodyParamsDefinition"
}
}
],
"responses": {
"200": { ?? } ...
可以生成兩種內容類型:
GET /some/endpoint
默認響應是一個 csv 文件,但如果像這樣使用format
查詢參數/some/endpoint?format=json
,則響應將采用 json 格式。
我很難找到如何以正確的響應完成我的規范。 當我使用這種方法時: https://swagger.io/docs/specification/describing-responses/我得到一個驗證錯誤: ...get.responses['200'] should NOT have additional properties
您就快完成了,您只需要為響應定義一個schema
。 此schema
定義與此狀態代碼關聯的所有內容類型的響應結構。
例如,如果操作返回此 JSON:
[
{
"petType": "dog",
"name": "Fluffy"
},
{
"petType": "cat",
"name": "Crookshanks"
}
]
和這個CSV:
petType,name
dog,Fluffy
cat,Crookshanks
你會使用:
# YAML
responses:
200:
description: OK
schema:
type: array
items:
type: object
properties:
petType:
type: string
name:
type: string
更多信息:描述響應
在 OpenAPI 3.0 中,改進了內容類型定義,並且架構可以因內容類型而異:
openapi: 3.0.0
...
paths:
/some/endpoint:
get:
responses:
'200':
description: OK
content:
# JSON data is an object
application/json:
schema:
type: object
properties:
message:
type: string
# CSV data is a string of text
text/csv:
schema:
type: string
GET /some/endpoint
默認響應是一個 csv 文件,但如果像這樣使用format
查詢參數/some/endpoint?format=json
,則響應將采用 json 格式。
目前無法將特定響應映射到特定操作參數,但 OpenAPI 規范存儲庫中有幾個相關的建議:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.