OpenAPI/Swagger:响应中的数组

9nvpjoqh  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(126)

我想返回路径中的Activity列表。我试图通过在items属性中定义Activity类型来实现这一点:

#...
# intendation is not correct here
responses:
  '200':
    description: Successful response
    content:
      application/json:
        schema:
          type: array
          items:
            $ref: '#/components/responses/Activity'
#...
components:
  responses:
    Activity:
      description: Activity object
      content:
        application/json:
          schema:
            type: object
            properties:
              id:
                type: integer
                format: int64
              author_id:
                type: integer
                format: int64
              amount:
                type: integer
                format: int64
              activity_type:
                $ref: '#/components/schemas/ActivityType'
              start_time:
                type: string
                format: date
              end_time:
                type: string
                format: date

字符串
但这不会导致Activity列表,而是导致:

[
  "string"
]


我如何修复这个/实现一个Activity列表作为返回值?我已经尝试搜索文档。

enxuqcxy

enxuqcxy1#

response引用接受一个Response Object,其中包含contentdescription等属性。响应对象的主体或schema应该指向另一个架构对象,或者可以内联定义。在您的示例中,指向#/components/schemas/...是定义type: arrayitems关键字的正确方法,因为你想要一个收集响应

responses:
  '200':
    $ref: '#/components/responses/Activity_Response'
#...
components:
  schemas:
    Activity:
      description: Activity object
      schema:
        type: object
        properties:
          id:
            type: integer
            format: int64
          author_id:
            type: integer
            format: int64
          amount:
            type: integer
            format: int64
          activity_type:
            $ref: '#/components/schemas/ActivityType'
          start_time:
            type: string
            format: date
          end_time:
            type: string
            format: date
  responses:
    Activity_Response:
      description: Successful Activity response
      content:
        application/json:
          schema:
            type: array
            items:
              - $ref: '#/components/schemas/Activity'

字符串

相关问题