简体   繁体   English

如何在 OpenAPI 中定义嵌套数组?

[英]How to define a nested array in OpenAPI?

I want to define this我想定义这个

     "locationPoly": [
          [-87.63466, 24.50182],
          [-80.03074, 24.50182],
          [-80.03074, 31.00107],
          [-87.63466, 31.00107],
          [-87.63466, 24.50182]
        ],

In my OpenAPI YAML schema, I defined it like this:在我的 OpenAPI YAML 模式中,我这样定义它:

                  locationPoly:
                    type: array
                    properties:
                      type: array
                      properties:
                        type: number

But I am getting the "schema is invalid" error.但我收到“架构无效”错误。 Is there another way to define this?还有另一种定义方式吗?

Arrays must use the items keyword. Arrays 必须使用items关键字。

locationPoly:
  type: array
  items:
    type: array
    items:
      type: number

If the inner arrays always contain 2 elements, you can add minItems: 2 and maxItems: 2 for documentation purposes:如果内部 arrays 始终包含 2 个元素,则可以添加minItems: 2maxItems: 2用于文档目的:

locationPoly:
  type: array
  items:
    type: array
    items:
      type: number
    minItems: 2
    maxItems: 2

In your example, the locationPoly array appears to contain coordinates.在您的示例中, locationPoly数组似乎包含坐标。 A pair of coordinates can be considered a tuple (ordered list).一对坐标可以被认为是一个元组(有序列表)。 If you're using OpenAPI 3.1 (or will be using it in the future), it has the prefixItems keyword to define tuples.如果您使用的是OpenAPI 3.1 (或将来会使用它),它具有用于定义元组的prefixItems关键字。 This way you can provide separate descriptions for individual elements of the tuple (ie, for the 1st and 2nd items of the inner arrays in your example).这样,您可以为元组的各个元素提供单独的描述(即,在您的示例中,对于内部 arrays 的第一项和第二项)。

# openapi: 3.1.0

  locationPoly:
    type: array
    items:
      $ref: '#/components/schemas/Coordinates'
...

Coordinates:
  type: array
  prefixItems:
    # The 1st item
    - type: number
      description: Latitude
      minimum: -90
      maximum: 90
    # The 2nd item
    - type: number
      description: Longitude
      minimum: -180
      maximum: 180
  minItems: 2
  maxItems: 2
  additionalItems: false # Can be omitted if maxItems is specified

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM