Swagger:如何让属性引用OpenAPI 2.0中的模型(即嵌套模型)? [英] Swagger: How to have a property reference a model in OpenAPI 2.0 (i.e. nest the models)?
本文介绍了Swagger:如何让属性引用OpenAPI 2.0中的模型(即嵌套模型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难弄清楚如何在OpenAPI 2.0中嵌套模型.
I'm having a hard time trying to figure out how I can nest models in OpenAPI 2.0.
目前,我有:
SomeModel:
properties:
prop1:
type: string
prop2:
type: integer
prop3:
type:
$ref: OtherModel
OtherModel:
properties:
otherProp:
type: string
我尝试了许多其他方式:
I have tried many other ways:
prop3:
$ref: OtherModel
# or
prop3:
schema:
$ref: OtherModel
# or
prop3:
type:
schema:
$ref: OtherModel
以上似乎都不起作用.
但是,使用数组可以正常工作:
However, with arrays works just fine:
prop3:
type: array
items:
$ref: OtherModel
推荐答案
在OpenAPI 2.0中对其建模的正确方法是:
The correct way to model it in OpenAPI 2.0 would be:
swagger: '2.0'
...
definitions:
SomeModel:
type: object
properties:
prop1:
type: string
prop2:
type: integer
prop3:
$ref: '#/definitions/OtherModel' # <-----
OtherModel:
type: object
properties:
otherProp:
type: string
如果您使用OpenAPI 3.0,则模型将驻留在components/schemas
中,而不是在definitions
中:
If you use OpenAPI 3.0, models live in components/schemas
instead of definitions
:
openapi: 3.0.1
...
components:
schemas:
SomeModel:
type: object
properties:
prop1:
type: string
prop2:
type: integer
prop3:
$ref: '#/components/schemas/OtherModel' # <-----
OtherModel:
type: object
properties:
otherProp:
type: string
请记住将type: object
添加到对象模式,因为不会从其他关键字推断类型.
Remember to add type: object
to your object schemas because the type is not inferred from other keywords.
这篇关于Swagger:如何让属性引用OpenAPI 2.0中的模型(即嵌套模型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文