问题描述
我通过SwaggerHub进行了新的OpenAPI设置.是否可以选择全局强制某个Accept
标头?
I have a new OpenAPI setup via SwaggerHub. Is there an option to force a certain Accept
header globally?
我已经在响应中设置了Content-Type
:
I have set up the Content-Type
on the response:
openapi: 3.0.0
paths:
/test-path:
get:
responses:
'200':
description: OK
content:
application/vnd.company.v1.0.0+json:
通过cURL请求插入不同的Accept
标头时,将显示以下内容:
When inserting a different Accept
header via cURL request, the following out is made:
{"message":"Missing matching response for specified Accept header"}
这是有道理的,因为我们没有对此提供任何答复.
That makes sense, since we aren't providing any response for that.
推荐答案
与OpenAPI/Swagger 2.0不同,后者具有全局consumes
和produces
,OpenAPI 3.0要求在每个操作中分别定义请求和响应媒体类型.无法全局定义Content-Type
或请求或响应.
Unlike OpenAPI/Swagger 2.0, which has global consumes
and produces
, OpenAPI 3.0 requires that request and response media types be defined in each operation individually. There's no way to define the Content-Type
or requests or responses globally.
但是,您可以$ref
常见的响应定义(例如错误响应),这样可以减少重复.
You can, however, $ref
common response definitions (such as error responses), which can reduce the repetition.
openapi: 3.0.2
...
paths:
/foo:
get:
responses:
'400':
$ref: '#/components/responses/ErrorResponse'
/bar:
get:
responses:
'400':
$ref: '#/components/responses/ErrorResponse'
components:
responses:
ErrorResponse:
description: An error occurred
content:
application/vnd.error+json:
schema:
...
这篇关于如何在OpenAPI 3.0中全局设置Accept标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!