如何为具有多种类型的该字段编写 OpenAPI (Swagger) 定义?

How to write an OpenAPI (Swagger) definition for this field with multiple types?

我正在为 params 字段编写一个 OpenAPI 定义,它是一个对象并包含一个名为 name 的字段,默认情况下它是字符串类型,但可以是任何类型,例如整数、数字、布尔值、字符串或字符串、布尔值、数字、整数数组。

参数:{ [名称:字符串]:整数 |字符串 |编号 |布尔 |整数[] |字符串[] |数字[] |布尔值[] }

如何在 OpenAPI 中定义这样的字段?

我试过以下方法

  params:
    description: Simple parameters map
    type: object
    additionalProperties:
      name:
        type: object
        oneOf:
          - type: string
          - type: boolean
          - type: integer
          - type: number
          - type: array
            items:
              - string
              - integer
              - number
              - boolean

但这会产生以下语法错误:

Should not include additional properties name.

params which is an object and includes field called name which is of string type by default and it could be of any type like mention below:. Integer, number, boolean string or array of string, boolean, number, integer. it could be anything.

"" 是通过根本不指定 type 来定义的。但是在这种情况下,可能的 "anything" 值包括您没有提到的对象和对象数组。

params:
  description: Simple parameters map
  type: object
  properties:
    name: {}

    # OR if you want to add a description, use
    # name:
    #   description: Can be anything


但是,如果 "anything" 仅表示您列出的特定类型,则需要 anyOf。请注意,anyOf 在 OpenAPI 3.0 (openapi: 3.0.0) 中受支持,在 OpenAPI/Swagger 2.0 (swagger: "2.0") 中不受支持。

# openapi: 3.0.0

params:
  type: object
  properties:
    name:
      anyOf:
        - type: string
        - type: integer
        - type: number
        - type: boolean
        - type: array
          items:
            type: string
        - type: array
          items:
            type: integer
        - type: array
          items:
            type: number
        - type: array
          items:
            type: boolean