How to set the Accept header globally in OpenAPI 3.0?

会有一股神秘感。 提交于 2020-03-16 05:40:06

问题


I have a new OpenAPI setup via SwaggerHub. Is there an option to force a certain Accept header globally?

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:

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.


回答1:


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.

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:
            ...


来源:https://stackoverflow.com/questions/54145884/how-to-set-the-accept-header-globally-in-openapi-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!