components/schema 之外的可重用组件?
Reusable component outside of components/schema?
在 components/schema
之外还有其他地方可以放置可重用的组件吗?
我有一个 "partial"(因为没有更好的术语),我想 $ref
其他地方,但我不想 "Model":
一些例子:
NotFoundError:
type: object
properties:
code:
type: integer
format: int32
example: 404
message:
type: string
example: "Resource Not Found"
ID-Array:
type: array
items:
$ref: '#/components/schemas/ID'
writeOnly: true
ID:
type: integer
format: int32
example: 1
ID-ReadOnly:
allOf:
- $ref: '#/components/schemas/ID'
- readOnly: true
ID-WriteOnly:
allOf:
- $ref: '#/components/schemas/ID'
- writeOnly: true
一个可能的解决方案是将部分放在 extension key 下,比如 x-partials
,并相应地更改 $ref
。 $ref
不关心路径是什么,只要目标是预期的格式即可。
x-partials:
ID:
type: integer
format: int32
example: 1
components:
schemas:
ID-Array:
type: array
items:
$ref: '#/x-partials/ID'
writeOnly: true
ID-ReadOnly:
allOf:
- $ref: '#/x-partials/ID'
- readOnly: true
ID-WriteOnly:
allOf:
- $ref: '#/x-partials/ID'
- writeOnly: true
这样,例如,部分将不会显示在 Swagger UI 的 "Models" 部分中。
在 components/schema
之外还有其他地方可以放置可重用的组件吗?
我有一个 "partial"(因为没有更好的术语),我想 $ref
其他地方,但我不想 "Model":
一些例子:
NotFoundError:
type: object
properties:
code:
type: integer
format: int32
example: 404
message:
type: string
example: "Resource Not Found"
ID-Array:
type: array
items:
$ref: '#/components/schemas/ID'
writeOnly: true
ID:
type: integer
format: int32
example: 1
ID-ReadOnly:
allOf:
- $ref: '#/components/schemas/ID'
- readOnly: true
ID-WriteOnly:
allOf:
- $ref: '#/components/schemas/ID'
- writeOnly: true
一个可能的解决方案是将部分放在 extension key 下,比如 x-partials
,并相应地更改 $ref
。 $ref
不关心路径是什么,只要目标是预期的格式即可。
x-partials:
ID:
type: integer
format: int32
example: 1
components:
schemas:
ID-Array:
type: array
items:
$ref: '#/x-partials/ID'
writeOnly: true
ID-ReadOnly:
allOf:
- $ref: '#/x-partials/ID'
- readOnly: true
ID-WriteOnly:
allOf:
- $ref: '#/x-partials/ID'
- writeOnly: true
这样,例如,部分将不会显示在 Swagger UI 的 "Models" 部分中。