OpenAPI 3 - 属性的重用

Han*_*nes 4 swagger openapi

以下是 OpenAPI 3 定义的示例:

components:
  schemas:
    Foo:
      properties:
        string:
          type: string
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']
    Bar:
      properties:
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']
Run Code Online (Sandbox Code Playgroud)

有没有办法重用enumField或者我每次使用时都必须指定它?

Gab*_*res 9

我不知道是否可以引用单个属性,但您可以使用模式并通过拆分它来实现。

以下是您可以做什么的基本结构示例:

SchemaBase:
  type: object
  properties:
    foo:
      type: string

SchemaFull:
  allOf:
    - $ref: '#/components/schemas/SchemaBase'
    - type: object
      properties:
        bar:
          type: string
Run Code Online (Sandbox Code Playgroud)

在你的情况下可能是这样的:

components:
  schemas:
    EnumField:
      properties:
        enumField:
          type: string
          enum: ['VALUE_1', 'VALUE_2']
    Foo:
      allOf:
        - $ref: '#/components/schemas/EnumField'
        - properties:
            string:
              type: string
    Bar:
      allOf:
        - $ref: '#/components/schemas/EnumField'
Run Code Online (Sandbox Code Playgroud)