【问题标题】:Create complex types (definitions) in Swagger [duplicate]在 Swagger 中创建复杂类型(定义)
【发布时间】:2015-12-06 17:59:00
【问题描述】:

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

parameterspaths 上,我不能使用在定义中创建的类型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

但是出现这个错误:

招摇错误: 数据与“anyOf”中的任何模式都不匹配。

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

【问题讨论】:

    标签: yaml swagger swagger-2.0 swagger-editor


    【解决方案1】:

    请改用$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

    【讨论】:

    • 嗨,谢谢你的回答......它适用于这个例子,但现在我不能在路径参数中使用 $ref 。我举了上面的例子。你知道如何在路径参数中引用这种类型吗?问候。
    • 路径参数不能使用model。路径参数应该是原始类型,例如字符串、数字等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2020-08-19
    • 2011-10-05
    • 1970-01-01
    相关资源
    最近更新 更多