如何在OpenAPI 3中定义另一个模式的数组? [英] How to define an array of another schema in OpenAPI 3?

查看:309
本文介绍了如何在OpenAPI 3中定义另一个模式的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下架构:

User:
  type: object
  required:
    - id
    - username
  properties:
    id:
      type: integer
      format: int32
      readOnly: true
      xml:
        attribute: true
      description: The user ID
    username:
      type: string
      readOnly: true
      description: The username
    first_name:
      type: string
      description: Users First Name
    last_name:
      type: string
      description: Users Last Name
    avatar:
      $ref: '#/components/schemas/Image'
  example:
    id: 10
    username: jsmith
    first_name: Jessica
    last_name: Smith
    avatar: image goes here
  xml:
    name: user

效果很好. GET /user/{id}调用可以很好地显示示例数据.

Works great. The GET /user/{id} call displays the sample data just fine.

我有第二个模式,可以创建上述模式的数组:

I have a second schema that creates an array of the above schema:

ArrayOfUsers:
  type: array
  items:
    type: object
    required:
      - id
      - username
    properties:
      id:
        type: integer
        format: int32
        xml:
          attribute: true
        description: The user ID
      username:
        type: string
        description: The username
      first_name:
        type: string
        description: Users First Name
      last_name:
        type: string
        description: Users Last Name
      avatar:
        $ref: '#/components/schemas/Image'

这也很好. GET /user调用可以在数组中正确显示结构.

This also works great. The GET /user call displays the proper structure in an array just fine.

但我不想重复定义此架构.

But I'd rather not define this schema twice.

我想创建一个利用第一个模式并粘贴在数组中的模式.

I would like to create a schema that utilizes the first one and stick in an array.

我这次尝试失败了.

我这样尝试过:

UserArray:
  type: array
  items:
    type: object
    required:
      - id
      - username
  properties:
    things:
      type: array
      items:
        oneOf:
          - $ref: '#/components/schemas/User'

这种尝试给了我一个空数组:

This attempt gives me an empty array:

[
  {}
]

这不是我想要的结果.

对此有任何提示吗?

推荐答案

User对象的数组定义如下:

An array of User objects is defined as follows:

UserArray:
  type: array
  items:
    $ref: '#/components/schemas/User'

这篇关于如何在OpenAPI 3中定义另一个模式的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆