在OpenAPI中,如何使引用模式的某些字段成为必需的



我定义了模型A,它有两个字段,field1是必需的。

A:
type: object
required:
- field1
properties:
field1:
type: string
field2:
type: string

我想创建一个新的模式B,它具有与a相同的字段,但这两个字段都是必需的。

我可以这样写吗?

B:
allOf:
- $ref: '#/components/schemas/A'
- type: object
required:
- field2

是的,这正是你要做的。注意,您不需要在模式B中指定CCD_ 2,因为CCD_;"父";模式A.

B:
allOf:
- $ref: '#/components/schemas/A'
- required:
- field2

required也可以放在allOf旁边,而不是作为子模式:

B:
allOf:
- $ref: '#/components/schemas/A'
required:
- field2

OpenAPI 3.1中,模式中的$ref允许同级关键字,因此以下内容将起作用:

B:
$ref: '#/components/schemas/A'
required:
- field2

最新更新