问题描述
引用 OpenAPI 2.0,架构对象或 Swagger 2.0,架构对象,discriminator
字段的定义为:
Referencing OpenAPI 2.0, Schema Object, or Swagger 2.0, Schema Object, and the definition of discriminator
field as:
我的困惑/问题:
- 这对我来说是模棱两可的,它究竟在继承或多态性中起什么作用.有人可以用一个工作示例解释
discriminator
的确切作用,如果不使用它怎么办?在某些操作中是否有任何错误,警告或依赖它的工具? - 是否 swagger-editor 不支持
discriminator
,并且使用了此字段在其他一些工具中?
- It is ambiguous to me, what role exactly it plays in inheritance or polymorphism. Could some one please explain
discriminator
with a working example showing what it exactly does and what if we do not use it? Any errors, warnings or any tools that depends on it for some operations? - Is it the case that swagger-editor does not support
discriminator
, and this field is used in some other tools?
到目前为止,我已经尝试过:
- 我尝试使用 swagger-editor 和同一文档中的示例(也在下面提到) ,请尝试使用此属性,看看是否可以看到其任何特殊行为.我更改了属性,将其删除,然后将
Dog
模型扩展到了一个更高的级别,并在新的子模型上进行了尝试,但是在 swagger编辑器. - 我尝试在线搜索,特别是stackoverflow问题,但没有找到任何相关信息.
- I have tried to use swagger-editor and the example from the same documentation (also mentioned below), to play around with this property to see if I can see any of its special behaviors. I changed the property, removed it, and extend the
Dog
model to one level deeper and tried the same on the new sub-model, but I did not see any changes in the preview of swagger-editor. - I tried searching online, and specially stackoverflow questions, but did not find any relevant information.
我用来做实验的示例代码:
definitions:
Pet:
type: object
discriminator: petType
properties:
name:
type: string
petType:
type: string
required:
- name
- petType
Cat:
description: A representation of a cat
allOf:
- $ref: '#/definitions/Pet'
- type: object
properties:
huntingSkill:
type: string
description: The measured skill for hunting
default: lazy
enum:
- clueless
- lazy
- adventurous
- aggressive
required:
- huntingSkill
Dog:
description: A representation of a dog
allOf:
- $ref: '#/definitions/Pet'
- type: object
properties:
packSize:
type: integer
format: int32
description: the size of the pack the dog is from
default: 0
minimum: 0
required:
- packSize
推荐答案
根据此 Google网上论坛,discriminator
用于allOf
属性的顶部,并且在多态的超类型中进行了定义.如果不使用discriminator
,则allOf
关键字描述一个模型包含其他模型的组成属性.
According to this google group, discriminator
is used on top of the allOf
property and it is defined in the super type for polymorphism. If discriminator
is not used, the allOf
keyword describes that a model contains the properties of other models for composition.
就像在您的示例代码中一样,Pet
是具有petType
属性的超类型,其标识为discriminator
,而Cat
是Pet
的子类型.以下是Cat
对象的json示例:
Just like in your sample code, Pet
is a super type with property of petType
identified as the discriminator
and Cat
is a sub type of Pet
. Following is a json example of a Cat
object:
{
"petType": "Cat",
"name": "Kitty"
}
使用discriminator
旨在指示用于标识对象类型的属性.假设有一些工具可以使用discriminator
适当支持定义对象,则可以通过扫描属性来确定类型.例如,根据petType
标识对象是Cat
.
The use of discriminator
intends to indicate the property used to identify the type of an object. Assumes that there are tools that can proper support definition objects with the use of discriminator
, it is possible to determine the type by scanning the property. For example, identify the object is a Cat
according to petType
.
但是,discriminator
字段在当前版本的规范或示例中定义不正确(请参见问题#403 ).据我所知,目前Swagger提供的工具还没有适当地支持它.
However, the discriminator
field is not well defined in the current version's specification or the samples (see issue #403). As far as I know, there is no tools provided by Swagger properly support it at the time being.
discriminator
.在这种情况下,它很自然,可以用作其他开发人员了解多态关系的指标.如果支持discriminator
的第三方工具(如 ReDoc )(请参见此 gif 和示例),您可能会发现这很有用.
discriminator
may be used if the model has a property used to determine the type. In this case, it is naturally fit and it can be used as an indicator for other developers understand the polymorphism relationship. If third party tools like ReDoc which support discriminator
(see petType
in this gif and example) is considered, you may find this useful.
这篇关于“判别器"在多态性方面,OpenAPI 2.0(Swagger 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!