【问题标题】:Curl failing with this swagger spec使用这个招摇规范的卷曲失败
【发布时间】:2021-02-02 11:37:06
【问题描述】:

我已经创建了一个招摇规范

/config/download:
 post:
  summary: Download config
  consumes:
    - application/json
  produces:
    - application/zip
  parameters:
    - in: body
      name: config
      schema:
        type: array
        items:
          title: config files
          description: All config file name that need to be downloaded
          type: string
        minItems: 0
        maxItems: 1024
  responses:
    200:
      description: 
      schema:
        type: string
        format: binary

我正在通过 go-swagger 编译这个 swagger 规范,我也为此实现了后端

但是当我尝试卷曲这个请求时,我得到了

 curl -i  http://127.0.0.1:<port>/config/download -X "POST" -d '{"config": ["config1.json", "config.json"]}' -H "Content-Type: application/json"
HTTP/1.1 400 Bad Request
Content-Type: application/json
Date: Tue, 02 Feb 2021 07:36:06 GMT
Content-Length: 132
Connection: close

{"code":400,"message":"parsing config body from \"\" failed, because json: cannot unmarshal object into Go value of type []string"}

我认为 curl 没有问题,但 swagger 规范似乎也是正确的,那么可能是什么问题。从 curl 错误消息中可以清楚地看出,服务器没有正确处理这个请求,甚至没有到达后端实现代码。

欢迎提出任何建议。

【问题讨论】:

    标签: go curl swagger swagger-2.0 go-swagger


    【解决方案1】:

    您应该为您的请求正文参数创建一个新定义,例如

    config:
        title: config names
        properties:
          configs:
            title: config names
            description: List of config files to download.
            type: array
            items:
              title: Config
              type: string
            minItems: 0
            maxItems: 1024
    

    然后招摇规范看起来像这样

           parameters:
            - in: body
              name: config
              schema:
                type: object
                $ref: '#/definitions/config'
    
    

    【讨论】:

      【解决方案2】:

      尝试在响应定义中添加一个 object 类型的 body 元素,如下所示:

        parameters:
        - name: body
          in: body
          type: object
          required: true
          properties:
            config:
              type: array
              items:
                title: config files
                description: All config file name that need to be downloaded
                type: string
              minItems: 0
              maxItems: 1024
        responses:
      

      【讨论】:

      • 似乎在 Swagger "2.0" 规范中我们可以将属性放在正文中。收到此错误“正文中的 .post.parameters.properties 是禁止的属性”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2022-08-22
      相关资源
      最近更新 更多