在 Swagger 中创建复杂类型(定义)

Create complex types (definitions) in Swagger

我创建了一个名为 Product 的定义和另一个名为 Text 的定义(参见代码)。

pathsparameters 上,我无法使用在定义中创建的类型 Text。在定义 Product 上,我有一个名为 message 的 属性,我希望 属性 也是类型 Text

(...)

paths:
  /products:
    get:
      summary: Product Types
      description: |
        Description text
      parameters:
        - name: latitude
          in: query
          description: Latitude component of location.
          required: true
          ### The type Text was not found here
          type: Text ### The type Text was not found here
(...)

definitions:

  Product:
    properties:
      message:
        ### The type Text was not found here
        type: Text ### Compilation Error in this line ####
      name:
        type: string
        description: Data description.


  Text:
    properties:
      code:
        type: string

但是出现这个错误:

Swagger Error: Data does not match any schemas from 'anyOf'.

如何在类型 Product 上引用类型 Text

请改用$ref。这是一个例子

type: object
required:
- name
properties:
  name:
    type: string
  address:
    $ref: '#/definitions/Address'
  age:
    type: integer
    format: int32
    minimum: 0

参考:https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#simple-model