Api 如何指定对象';s类型是几种可能的对象类型之一?
我有一个API,在这里我向服务器发布一组对象,按类型区分,其中每种类型都有一些不同的参数。命令的结构如下所示:Api 如何指定对象';s类型是几种可能的对象类型之一?,api,rest,swagger,swagger-ui,swagger-2.0,Api,Rest,Swagger,Swagger Ui,Swagger 2.0,我有一个API,在这里我向服务器发布一组对象,按类型区分,其中每种类型都有一些不同的参数。命令的结构如下所示: { "type": <command-name>, "args": { <command-specific args> } } 我如何在招摇过市中指定这一点?我可以为“type”指定枚举,但是如何说““args”是这些可能的对象之一” 我已经退房了,但没什么特别的。最有希望的是allOf,因为它在编辑器中显示得很好(,
{
"type": <command-name>,
"args": {
<command-specific args>
}
}
我如何在招摇过市中指定这一点?我可以为“type”
指定枚举,但是如何说““args”
是这些可能的对象之一”
我已经退房了,但没什么特别的。最有希望的是allOf
,因为它在编辑器中显示得很好(,粘贴到):
看起来是这样的:
然而,这在语义上并不是我所需要的,而且,毫不奇怪,在中(没有为我的测试用例设置,不知道如何轻松链接本地文件),它们的显示方式就像所有字段同时出现一样,这当然是allOf
的意思:
用Swagger表示这一点的正确方法是什么?根据Ron on的输入,我想要的是使用鉴别器
属性:
definitions:
Product:
type: object
discriminator: type
properties:
type: string
required: [type]
Flooblinate:
allOf:
- $ref: '#/definitions/Product'
- type: object
properties:
intensity:
type: string
frequency:
type: string
Blagostrate:
allOf:
- $ref: '#/definitions/Product'
- type: object
properties:
temperature:
type: number
darkMatter:
type: boolean
从语义上来说,这意味着我想要它的意思。我应该指定$ref:“#/definitions/Product”
,只要API接受或返回flooblitate
或Blagostrate
中的任何一个。注意:这需要在对象上有一个字段(此处称为type
)作为鉴别器
我认为这可能是一种方法,但这些工具并没有显示出我的预期。然而:
这是因为这些工具还没有100%支持鉴别器——然而,这是描述您的用例的正确方式。
一旦你在顶级模型中定义了鉴别器,任何允许它的东西都将被认为是一个可行的选择,事实上,你正在参考顶级模型来使用它
我在这里看到的问题是discriminator:type必须匹配每个产品子对象的名称。您需要让产品持有一个“类型”字段,其中的值与子对象的名称紧密耦合[Flooblinate,BlagoState],如果名称发生变化,这将非常糟糕。你有办法解决这个问题吗?
definitions:
Product:
type: object
allOf:
- type: object
title: Flooblinate
properties:
intensity:
type: string
frequency:
type: string
- type: object
title: Blagostrate
properties:
temperature:
type: number
darkMatter:
type: boolean
definitions:
Product:
type: object
discriminator: type
properties:
type: string
required: [type]
Flooblinate:
allOf:
- $ref: '#/definitions/Product'
- type: object
properties:
intensity:
type: string
frequency:
type: string
Blagostrate:
allOf:
- $ref: '#/definitions/Product'
- type: object
properties:
temperature:
type: number
darkMatter:
type: boolean