Swagger 如何重构这个招摇过市的API规范

Swagger 如何重构这个招摇过市的API规范,swagger,swagger-ui,swagger-2.0,swagger-editor,Swagger,Swagger Ui,Swagger 2.0,Swagger Editor,我有几个端点,其中有一些标准错误响应,如404、401、403和默认值。我想将这些响应重构为一个夸张的定义,但我无法实现这一点。我尝试了一些技巧,但总是导致解析错误。这是我的yaml swagger: '2.0' info: title: My API description: My API description version: 0.0.1 host: api.example.com schemes: - https basePath: / produces: - app

我有几个端点,其中有一些标准错误响应,如
404
401
403
默认值
。我想将这些响应重构为一个夸张的定义,但我无法实现这一点。我尝试了一些技巧,但总是导致解析错误。这是我的yaml

swagger: '2.0'
info:
  title: My API
  description: My API description
  version: 0.0.1
host: api.example.com
schemes:
  - https
basePath: /
produces:
  - application/json
paths:
  /users:
    get:
      operationId: getUsers
      summary: Get users
      description: Get all users
      tags:
        - Users
      responses:
        '200':
          description: An array of users
          schema:
            type: array
            items:
              $ref: '#/definitions/User'
        '401':
          description: Authentication required
          schema:
            $ref: '#/definitions/Error'
        '402':
          description: Payment required
          schema:
            $ref: '#/definitions/Error'
        '403':
          description: Unauthorized access
          schema:
            $ref: '#/definitions/Error'
        '404':
          description: Not found
          schema:
            $ref: '#/definitions/Error'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'
  /games:
    get:
      operationId: getGames
      summary: Get games
      description: Get all games
      tags:
        - Games
      responses:
        '200':
          description: An array of games
          schema:
            type: array
            items:
              $ref: '#/definitions/Game'
        '401':
          description: Authentication required
          schema:
            $ref: '#/definitions/Error'
        '402':
          description: Payment required
          schema:
            $ref: '#/definitions/Error'
        '403':
          description: Unauthorized access
          schema:
            $ref: '#/definitions/Error'
        '404':
          description: Not found
          schema:
            $ref: '#/definitions/Error'
        default:
          description: Unexpected error
          schema:
            $ref: '#/definitions/Error'
definitions:
  User:
    type: object
    properties:
      id:
        type: integer
        description: Unique id of user
      name:
        type: string
        description: Name of user
  Game:
    type: object
    properties:
      id:
        type: integer
        description: Unique id of game
      name:
        type: string
        description: Name of game
  Error:
    type: object
    properties:
      code:
        type: integer
        description: HTTP status code
      message:
        type: string
        description: Message describing error

观察
/users
/games
中的重复响应。如何重构并将它们移动到
定义中?

您可以使用共享的
响应
对象来定义响应。然后在各个操作中引用它们。从“关于共享响应”对象:

一个对象,用于保存跨操作重用的响应。回应 定义可以引用到此处定义的定义


虽然这是一个有效的规范,但除了
default
response之外,Swagger用户界面将无法解析共享响应。以下是与此相关的问题。

这实际上并不能有效解决问题。我一直在寻找一个可以定义对象的东西,比如说
apirerrors
,具有
401
402
等属性,并在
响应
特定请求下继承该对象。好吧,这在当前规范中是不可能的。是的,看起来就是这样。目前,接受答案是最好的情况。