首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何为该字段编写具有多种类型的OpenAPI (Swagger)定义?

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

Stack Overflow用户
提问于 2019-05-17 20:13:05
回答 1查看 754关注 0票数 1

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

参数:{ name: string: int | string | number | boolean | int[] | string[] | number[] | boolean[] }

如何在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

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

不应包含其他属性名称。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-23 03:39:38

参数,它是一个对象,包括名为name的字段,默认情况下它是string类型,它可以是任何类型,如下所述:整数、数字、布尔字符串或字符串数组、布尔值、数字、整数。它可以是任何东西。

通过根本不指定type来定义"Can be anything“。但在本例中,可能的"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。请注意,OpenAPI3.0 (openapi: 3.0.0)支持anyOf,而OpenAPI2.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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56186187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档