gpt4 book ai didi

swagger - 如何在 OpenAPI (Swagger) 中指定多个 404 原因?

转载 作者:行者123 更新时间:2023-12-03 18:38:05 27 4
gpt4 key购买 nike

我正在为嵌套资源(属于交付的内容)定义路径。如果客户端收到 404,则可能是因为未找到交付 ID,或者交付不包含任何指定类型的内容。

如何使用 OpenAPI (YAML) 建模?

我现在有这个...

 paths:
'/deliveries/{id}/content/articles':
get:
summary: Retrieves articles from a delivery
description: Retrieves all articles from a single delivery
[...]
responses:
'200':
description: articles found
schema:
$ref: '#/definitions/Article'
'404':
description: delivery not found
schema:
$ref: '#/definitions/Error'
'404':
description: delivery did not contain any articles
schema:
$ref: '#/definitions/Error'

...但是当我从 Swagger 编辑器保存 JSON 时,它会删除除最后一个之外的所有 404 响应(“交付不包含任何文章”)。

最佳答案

OpenAPI/Swagger 2.0 中不允许每个状态码有多个响应类型,但在 OpenAPI 3.0 中支持 by using oneOf .

在 OpenAPI 2.0 中,404 响应只能有一个模式:

      responses:
'404':
description: delivery not found, or delivery did not contain any articles
schema:
$ref: '#/definitions/Error'

...
definitions:
Error:
type: object
properties:
status:
type: integer
type:
type: string
message:
type: string

哪里 Error有效载荷可以是,比如:

{
"status": 404,
"type": "DeliveryNotFoundError",
"message": "delivery not found"
}

{
"status": 404,
"type": "NoArticlesInDeliveryError",
"message": "delivery did not contain any articles"
}

关于swagger - 如何在 OpenAPI (Swagger) 中指定多个 404 原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40640669/

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