OpenAPI 3 specification of Option[]



假设我将这个case classes作为某个OAS3规范的一部分:

case class UserData(userId: String, country: Country)
case class Country(id: String, name: String)

那么,OAS3规范应该是这样的:

components:
schemas:
UserData:
type: object
properties:
userId:
type: string
country:
$ref: '#/components/schemas/Country'
Country:
type: object
properties:
id:
type: string
name:
type: string

我的问题是:如果一个国家作为选项,我该如何建模?

case class UserData(userId: String, country: Option[Country])
case class Country(id: String, name: String)

我试着把"nullable: true"但这是无效的:

components:
schemas:
UserData:
type: object
properties:
userId:
type: string
country:
nullable: true <---- NOT VALID
$ref: '#/components/schemas/Country'
Country:
type: object
properties:
id:
type: string
name:
type: string

我如何建模这个选项?

根据需要标记所有非可选参数:

components:
schemas:
UserData:
required:
- userId
type: object
properties:
userId:
type: string
country:
$ref: '#/components/schemas/Country'
Country:
required:
- id
- name
type: object
properties:
id:
type: string
name:
type: string

最新更新