昂首阔步地回应相关对象

时间:2018-06-19 15:50:36

标签: swagger swagger-ui api-design openapi

我将SwaggerHub与OpenAPI 3一起使用来定义API。一种路由GET /foo/{id]应该返回给定foo的{​​{1}}对象及其关联的id对象。该API将返回类似:bar的信息。即{id: 4, name: 'test', bars: [{id: 53, name: 'barName1'}, {id: 87, name: 'barName2'}]}foo之间存在多对多关系。

如何用OpenAPI 3语法描述这一点?我尝试使用anyOf属性。到目前为止,我有:

bar

但是,这似乎并未在UI中显示正确的架构(UI中未提及paths: /foo/{id}: get: parameters: - name: id in: path required: true schema: type: integer responses: '200': content: application/json: schema: type: array items: $ref: '#/components/schemas/Foo' anyOf: - $ref: '#/components/schemas/Bar' )。

1 个答案:

答案 0 :(得分:0)

您不需要anyOfanyOf表示备选方案(“ Foo或Bar”),而您具有通常的嵌套结构-具有属性Foo的对象bars,它是Bar的数组s。可以这样描述:

      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Foo'

components:
  schemas:
    Foo:
      type: object
      properties:
        bars:
          type: array
          items:
            $ref: '#/components/schemas/Bar'
      required:
        - bars
    Bar:
      ...