我如何定义一个对象可能是空的,或者必须包含至少两个必需的字段,如果它在OpenAPI 3.0中不是空的



我试图找到如何定义一个可以由以下对象之一表示的模型:

{
"name": "John Doe",
"additional_info": {
"scheme": {
"id": 1
},
"source": "direct"
}
}
{
"name": "John Doe",
"additional_info": {
"scheme": {
"id": 1
},
"source": "direct",
"optional_key": "something"
}
}
{
"name": "John Doe",
"additional_info": {}
}

所以我需要确保additional_info对象OR可能是空的,OR必须包含至少两个必需的键scheme(和scheme对象必须包含id键)和source, AND可以包含任何可选的键。

我尝试了以下方案:

model:
description: ''
content:
application/json:
schema:
allOf:
- type: object
properties:
name:
type: string
- oneOf:
- type: object
properties:
additional_info:
type: object
properties:
scheme:
type: object
required:
- id
properties:
id:
type: integer
source:
type: string
required:
- scheme
- source
- type: object
properties:
additional_info:
type: object
additionalProperties: false
examples:
example-1:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
example-2:
name: John Doe
additional_info: {}
example-3:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
optional_key: something

但我不确定它是否正确。

我尝试了不同的选项,并找到了正确的一个,它的工作。也许这是多余的,并且在某些地方您不需要使用minProperties和maxProperties关键字。

在这里:

model:
description: ''
content:
application/json:
schema:
allOf:
- type: object
properties:
name:
type: string
required:
- name
- oneOf:
- type: object
properties:
additional_info:
type: object
minProperties: 2
properties:
scheme:
type: object
required:
- id
properties:
id:
type: integer
source:
type: string
required:
- scheme
- source
- type: object
properties:
additional_info:
type: object
additionalProperties: false
minProperties: 0
maxProperties: 0
examples:
example-1:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
example-2:
name: John Doe
additional_info: {}
example-3:
name: John Doe
additional_info:
scheme:
id: 1
source: direct
optional_key: something

相关内容

  • 没有找到相关文章

最新更新