如何在 Open API 中将一个组件定义为另一个组件的数组?
How to define a component as an array of another component in Open API?
我想定义两个组件:一个用于单个学生,一个用于一组学生。但是我想遵循DRY的原则。所以我尝试了这个:
components:
schemas:
student:
type: object
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
students:
type: array
items:
$ref: '#/components/schemas/student/properties'
不过好像不行。 Swaggers 编辑器以这种方式呈现它:
如果我重复一遍:
components:
schemas:
student:
type: object
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
students:
type: array
items:
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
看起来是这样的:
当我尝试定义 named anchor 时,出现以下错误:
Structural error at components.schemas.student
should NOT have additional properties
additionalProperty: $id
如何正确引用另一个组件?
您的 $ref
级别太深了。您需要指向模式(或子模式)而不是模式中的 properties
对象。
students:
type: array
items:
$ref: '#/components/schemas/student'
我想定义两个组件:一个用于单个学生,一个用于一组学生。但是我想遵循DRY的原则。所以我尝试了这个:
components:
schemas:
student:
type: object
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
students:
type: array
items:
$ref: '#/components/schemas/student/properties'
不过好像不行。 Swaggers 编辑器以这种方式呈现它:
如果我重复一遍:
components:
schemas:
student:
type: object
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
students:
type: array
items:
properties:
StudentID:
type: integer
example: 3
StudentName:
type: string
example: David
StudentRemarks:
type: string
example: High Grade Student
看起来是这样的:
当我尝试定义 named anchor 时,出现以下错误:
Structural error at components.schemas.student should NOT have additional properties additionalProperty: $id
如何正确引用另一个组件?
您的 $ref
级别太深了。您需要指向模式(或子模式)而不是模式中的 properties
对象。
students:
type: array
items:
$ref: '#/components/schemas/student'