如果对象在 OpenAPI 3.0 中不为空,我如何定义该对象可能为空或必须包含至少两个必填字段
How do I define that an object may be empty or must contain at least two required fields if it is not empty in 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 可能为空,或者必须包含至少两个必需的键 scheme
(并且 scheme
对象必须包含 id
键)和 source
,并且可以包含任何可选键。
我尝试了以下方案:
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
我试图找到如何定义可以由以下对象之一表示的模型:
{
"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 可能为空,或者必须包含至少两个必需的键 scheme
(并且 scheme
对象必须包含 id
键)和 source
,并且可以包含任何可选键。
我尝试了以下方案:
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