Swagger Open API 3.0.1:如何为响应示例定义对象数组

问题描述

使用 Open API 3.0.1 为 API 规范定义 swagger 定义。但是,这里定义的响应示例 GoodResponse 没有呈现。虽然语法似乎是有效的。任何关于我做错了什么的指示表示赞赏

paths:
  /java/api/play:
    post:
      tags:
        - some-controller
      operationId: someOperation
      requestBody:
        content:
          application/xml:
            schema:
              $ref: '#/components/schemas/HarryKartType'
        required: true
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                items:
                  $ref: '#/components/schemas/GoodResponse'
components:
  schemas:
    GoodResponse:
      required:
        - goodstatusCode
        - goodresponseMessage
      type: object
      properties:
        goodstatusCode:
          type: integer
          example: 200
        goodresponseMessage:
          type: array
          example:
            - position: 1
              horseName: "TIMetoBELUCKY"
            - position: 2
              horseName: "HERCULES BOKO"
            - position: 3
              horseName: "CARGO DOOR"
          items:
             $ref: '#/components/schemas/GoodResponseMessage'
             
    GoodResponseMessage:
      type: object
      properties:
        position:
                type: integer
                example: 1
        horseName:
                type: string
                example: "TIMetoBELUCKY"

解决方法

您需要在 schema 内而不是在 items 内定义 $ref 试试这个:

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...