Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;鉴别器;在多态性方面,OpenAPI 2.0(Swagger 2.0)_Swagger_Swagger 2.0_Swagger Editor_Openapi_Swagger Tools - Fatal编程技术网

&引用;鉴别器;在多态性方面,OpenAPI 2.0(Swagger 2.0)

&引用;鉴别器;在多态性方面,OpenAPI 2.0(Swagger 2.0),swagger,swagger-2.0,swagger-editor,openapi,swagger-tools,Swagger,Swagger 2.0,Swagger Editor,Openapi,Swagger Tools,参考,或,以及鉴别器字段的定义为: 添加对多态性的支持。鉴别器是用于区分继承此架构的其他架构的架构属性名称。使用的属性名称必须在此架构中定义,并且必须位于required属性列表中。使用时,该值必须是此架构或继承它的任何架构的名称 我的困惑/问题: definitions: Pet: type: object discriminator: petType properties: name: type: string petTyp

参考,或,以及
鉴别器
字段的定义为:

添加对多态性的支持。鉴别器是用于区分继承此架构的其他架构的架构属性名称。使用的属性名称必须在此架构中定义,并且必须位于
required
属性列表中。使用时,该值必须是此架构或继承它的任何架构的名称

我的困惑/问题:

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
  • 对我来说,它究竟在继承或多态性中扮演什么角色是不明确的。请有人用一个工作示例解释一下鉴别器,说明它到底是做什么的,如果我们不使用它怎么办?在某些操作中是否存在任何错误、警告或依赖于它的任何工具
  • 是否是不支持鉴别器,并且此字段用于某些其他工具的情况
到目前为止我所尝试的:

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
  • 我试着使用同一文档(也在下面提到)中的示例来处理这个属性,看看是否可以看到它的任何特殊行为。我更改了属性,将其删除,并将
    Dog
    模型扩展到更深一层,并在新的子模型上尝试了相同的操作,但在预览中未看到任何更改
  • 我试着在网上搜索,特别是一些问题,但没有找到任何相关信息
我用来做实验的示例代码:

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
据此,
discriminator
用于
allOf
属性的顶部,并在多态性的超级类型中定义。如果未使用
鉴别器
,则
allOf
关键字描述一个模型包含其他模型的属性以进行合成

与示例代码一样,
Pet
是一个超级类型,其属性为
petType
,标识为
鉴别器
Cat
Pet
的子类型。以下是
Cat
对象的json示例:

{
  "petType": "Cat",
  "name": "‎Kitty"
}
使用
鉴别器
旨在指示用于识别对象类型的属性。假设有一些工具可以使用
鉴别器
正确支持定义对象,则可以通过扫描属性来确定类型。例如,根据
petType
识别对象为
Cat

但是,
鉴别器
字段在当前版本的规范或示例中没有很好的定义(请参阅)。据我所知,目前还没有由斯威格提供的工具能够正确地支持它


如果模型具有用于确定类型的属性,则可以使用
鉴别器。在这种情况下,它自然适合,可以作为其他开发人员了解多态关系的指标。如果考虑到第三方工具,例如支持
鉴别器
(请参阅本节和中的
petType
),您可能会发现这很有用。

鉴别器功能在OpenApi 3中得到了很大改进。现在提供一个鉴别器对象,其中包含鉴别器属性的名称,以及该属性的值到架构名称的映射

(我知道你问过OpenAPI2,但这在3中改进了很多,希望你能利用它)


请参阅:对于没有工具支持鉴别器时的实际规范

,这意味着没有必要使用它。如果您的架构具有用于标识类型的属性,则可以使用它。在这种情况下,
鉴别器
自然适合,可以帮助其他开发人员理解多态关系。如果您使用第三方工具,比如支持
鉴别器
,它可能会很有用。@wilson@musa。这里是ReDoc的作者。我可以补充的是,从工具作者的角度来看,
discriminator
确实很难使用。坦白地说,除了ReDoc,我想不起还有什么其他工具可以使用鉴别器:(这里是关于ReDoc中鉴别器是如何使用的gif链接:。@Wilson欢迎您将其附加到您的answer@RomanHotsiy感谢您的注释和gif的链接:)仅供参考:我是.net的一个招摇过市工具链的作者。库支持规范中的鉴别器和c#/typescript生成器。。。