多部分POST请求中参数的Swagger错误

时间:2017-05-15 17:01:39

标签: swagger swagger-2.0 swagger-editor

我使用Swagger编辑器来记录内置Node的现有API,但它一直给我以下错误:

  

paths./upload/Rate.post.parameters [0]上的模式错误       不是<#/ definitions / parameter>中的一个,<#/ definitions / jsonReference>

此错误出现在我的代码的3个位置:

  • paths./upload/Rate.post.parameters [0]
  • paths./upload/Rate.post.parameters [1]
  • paths./users/register.post.parameters [0]

我已经搜索了很多,但是,例如,这个链接没有解决我的问题,虽然它是同样的错误:

Swagger parameter error "is not exactly one from <#/definitions/parameter>"?

这是我的Swagger定义:

  /users/register:
    post:
      tags:
      - "users"
      description: Allows an user to register at the Server
      produces:
        - application/json
      parameters:
        - name: body
          in: body
          description: JSON Object with information for a Register request from an User
          schema: 
            type: object
            required:
              - username
              - password
              - weight
              - height
              - gender
              - restRate
              - age
            properties:
              username:
                type: string
              password:
                type:
                format: password
              weight:
                type: integer
              height:
                type: integer
              gender:
                type: string
              restRate:
                type: integer
              age: 
                type: integer
      # Expected responses for this operation:
      responses:
        # Response code
        200:
          description: Register successful
          schema:
            type: string

  /upload/Rate:
    post:
      tags:
      - "upload"
      description: Allows an user to upload a file into the server
      produces:
        - application/json
      consumes: 
        - multipart/form-data
      parameters:
        - name: body
          in: formData
          description: JSON Object with information for an upload request from an User
          required: false
          schema:
            type: object
            required:   
              - user
              - overlap
              - window 
              - latitude
              - longitude
              - time
              - date
            properties:
              user:
                type: string
              overlap:
                type: string
              window:
                type: string
              latitude:
                type: string
              longitude:
                type: string
              time:
                type: string
              date:
                type: string
        - name: files 
          in: formData
          description: File with the corresponding Rate
          required: false
          schema:
            type: object
            required:
              - rate
            properties:
              rate:
                type: file
      # Expected responses for this operation:
      responses:
        # Response code
        200:
          description: Login successful
          schema:
            type: string

也许这会有所帮助,但是一旦我解析了它的参数,后期路线(上传/费率)就会收到类似这样的请求:

user = req.body.user;
rr = req.files.rate;
overlap = req.body.overlap;
windowT = req.body.window;
latitude = req.body.latitude;
longitude = req.body.longitude;
time = req.body.time;
date = req.body.date;

感谢您的帮助和时间!

1 个答案:

答案 0 :(得分:0)

有很多问题。

/upload/register

  • 架构属性password缺少type
  • 的值
  • 属性名称不匹配 - restHR(位于properties)与restRate之间(位于required列表中)。

upload/Rate

  • 此处的问题是由multipart/form-data请求中的对象参数引起的。在OpenAPI(fka Swagger)2.0中,表单参数可以是原始值和数组,但不是对象。因此,无法使用OpenAPI 2.0描述您的示例。

    如果您正在设计新的API(或者您可以并且愿意将API实现更改为与OpenAPI 2.0兼容),可能的解决方案是:

    • 将操作更改为使用application/json并将所有输入参数合并到单个JSON对象中。 file参数需要实现为base64字符串 - type: string format: byte
    • 使用multipart/form-data但将对象参数拆分为单独的表单参数。


    即将推出的OpenAPI 3.0将支持表单数据中的对象参数:
    https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.md#special-considerations-for-multipart-content