【问题标题】:Swagger 2.0: How to specify an input parameter of type 'object'Swagger 2.0:如何指定“对象”类型的输入参数
【发布时间】:2015-06-19 14:26:56
【问题描述】:

使用 Swagger 2.0 我正在尝试指定对象类型的输入参数:

代码sn-p:

paths:
  '/thingies/{thingy_id}.json':
    put:
      summary: Update an existing thingy
      description: Updates an existing thingy
      parameters:
        - name: thingy_id
          description: ID of the thingy to update
          in: path
          required: true
          type: integer
        - name: translation
          description: Name and Locale for new translation
          in: formData
          type: object
          properties:
            name:
              type: string
            locale:
              type: string

但是验证器抱怨type: object 部分。

我应该如何正确指定我的输入参数?

【问题讨论】:

    标签: parameters swagger


    【解决方案1】:

    好的,感谢@ron 的输入,我已经找到了解决方案。是的,我需要使用body 而不是formData,但即便如此它也没有验证,抱怨type: object。但是,如果我先定义对象然后$ref 它那么一切正常。以下代码确实有效。

    swagger: '2.0'
    info:
      version: '1'
      title: Thingy Service
      description: Everyone loves their thingy
    schemes:
      - http
    consumes:
      - application/json
    produces:
      - application/json
    
    definitions:
      localisation:
        type: object
        required:
          - name
          - locale
        properties:
          name:
            type: string
          locale:
            type: string
    
    paths:
      '/thingies/{thingy_id}.json':
        put:
          summary: Update an existing thingy
          description: Updates an existing thingy
          parameters:
            - name: thingy_id
              description: ID of the thingy to update
              in: path
              required: true
              type: integer
            - name: translation
              description: Name and Locale for new translation
              in: body
              schema:
                $ref: '#/definitions/localisation'
          responses:
            204:
              description: No data
            404:
              description: Thingy not found
    

    【讨论】:

      【解决方案2】:

      Swagger 仅允许将对象输入作为主体参数。

      原因与内容的序列化方式有关,这取决于Content-Type 标头(Swagger 中的produces)。该标头与整个有效负载有关。

      在传递表单参数时,您可以使用以下两种 mime 类型之一:multipart/form-dataapplication/x-www-form-urlencoded。虽然前者允许您为每个部分指定 mime 类型,但 Swagger 当前不支持这样的定义。上面有一张公开票,允许它在未来版本的规范中使用。

      目前,在指定表单参数时,只能指定基元或基元数组。

      【讨论】:

      • 如果我将formData 更改为body,代码仍然无法验证。有什么建议吗?
      • 它没有验证,因为您需要在 schema 属性中包含定义,但我看到您已经弄清楚了。 body 参数的结构在这个意义上与其他参数不同因为它允许完整的模式定义。
      • 好的,谢谢。我经过反复试验得到了它:-)
      • 票号是多少?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2017-05-08
      • 1970-01-01
      相关资源
      最近更新 更多