How not to copy-paste 3 generic error responses in almost all paths?

前端 未结 2 1992
小鲜肉
小鲜肉 2020-12-03 17:55

I want almost all my paths to have the following 3 generic error responses. How do I describe that in Swagger without copypasting these lines everywhere?



        
相关标签:
2条回答
  • 2020-12-03 18:11

    It doesn't look like the Open API specification supports this.

    0 讨论(0)
  • 2020-12-03 18:20

    Looks like I can add the following global response definition:

    # An object to hold responses that can be used across operations.
    # This property does not define global responses for all operations.
    responses:
      NotAuthorized:
        description: The requester is unauthorized.
        schema:
          $ref: '#/definitions/Error'
    

    However I will still need to reference it in paths like this:

    401:
      $ref: '#/responses/NotAuthorized'
    


    Same thing in OpenAPI 3.0, except it uses #/components/responses/... instead of #/responses/...:

    openapi: 3.0.0
    
    # An object to hold responses that can be used across operations.
    # This property does not define global responses for all operations.
    components:
      responses:
        NotAuthorized:
          description: The requester is unauthorized.
          schema:
            $ref: '#/components/schemas/Error'
    
    # Then, in operation responses, use:
    ...
    401:
      $ref: '#/components/responses/NotAuthorized'
    


    There's also an open feature request in the OpenAPI Specification repository to add support for global/default responses for operations.

    0 讨论(0)
提交回复
热议问题