如何重用swagger定义并删除其中的一些参数? [英] How to reuse swagger definitions and remove some of the parameters in it?
问题描述
这是我的代码:
definitions:
User:
type: object
properties:
id:
type: integer
username:
type: string
first_name:
type: string
last_name:
type: string
password:
type: string
created_at:
type: string
format: date-time
updated_at:
type: string
format: date-time
required:
- username
- first_name
- last_name
- password
/api/users:
post:
description: Add a new user
operationId: store
parameters:
- name: user
description: User object
in: body
required: true
type: string
schema:
$ref: '#/definitions/User'
produces:
- application/json
responses:
"200":
description: Success
properties:
success:
type: boolean
data:
$ref: '#/definitions/User'
如您所见,在 /api/users
下的 post 键中,我使用了 User
定义作为我的架构.
As you can see, in the post key under /api/users
I used the User
definition as my schema on it.
我想减少我的代码,所以我重用了 User
定义作为我的模式.这里的问题是我不需要 id
、created_at
和 updated_at
字段.
I want to lessen my code so I reused the User
definition as my schema. The problem here is that I do not need the id
, created_at
and updated_at
fields.
除了提到的字段之外,有没有办法只继承一些字段?另外,由于我正在尝试学习 swagger,因此我希望有一些建议可以让它变得更好.谢谢.
Is there a way to just inherit some of the fields except the fields mentioned? Also, I would love some suggestions to make it better since I'm trying to learn swagger. Thank you.
推荐答案
如 此答案 类似问题:
您必须单独定义模型.
但是,您可以选择排除和差异的情况.
However, you have options for the cases of exclusion and difference.
如果您想排除,这是简单的情况,请创建一个模型使用排除的属性,比如 ModelA
.然后将 ModelB
定义为ModelA
加上附加属性:
If you're looking to exclude, which is the easy case, create a model
of with the excluded property, say ModelA
. Then define ModelB
as
ModelA
plus the additional property:
ModelB:
allOf:
- $ref: "#/definitions/ModelA"
- type: object
properties:
id:
type: string
如果您想定义差异,请遵循相同的方法上面,并从 ModelA
中排除 id.然后定义ModelB
和ModelC
作为扩展 ModelA
并向它们添加 id 属性,每个属性都有自己的限制.请注意,JSON Schema 可以让您遵循上面的原始示例在某些情况下覆盖"了定义.但是,由于它并不是真正的压倒一切,因此需要更好地理解 JSON Schema 的概念,不要简单化错误,我建议现在走这条路.
If you're looking to define the difference, follow the same method
above, and exclude the id from ModelA
. Then define ModelB
and ModelC
as extending ModelA
and add the id property to them, each with its own
restrictions. Mind you, JSON Schema can allow you to follow the
original example above for some cases to "override" a definition.
However, since it is not really overriding, and one needs to
understand the concepts of JSON Schema better to not make simple
mistakes, I'd recommend going this path for now.
这篇关于如何重用swagger定义并删除其中的一些参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!