How to define a property that can be string or null in OpenAPI (Swagger)?

旧巷老猫 提交于 2019-11-27 02:08:01

type as an array of types

type:
  - string
  - 'null'

is NOT valid in OpenAPI/Swagger (even though it's valid in JSON Schema). OpenAPI's type keyword requires a single type and cannot be an array of types.

Support for null depends on which version of OpenAPI you use:

  • In OpenAPI 3.0, use the nullable keyword to define nullable types:

    type: string
    nullable: true   # <----
    
  • OpenAPI 2.0 does not support null as the data type, so if you use 2.0, you are out of luck. You can only use type: string. That said, some tools support x-nullable: true as a vendor extension, even though nulls are not part of the OpenAPI 2.0 Specification.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!