swagger 不能导出代码的错误

问题描述

今天在导出swagger 的 client 代码时候,遇到一个报错

image.png

提示 there was an error generating the clients go code. please ensure that the definition and all external refs (inc. domains) are valid

这句报错非常模糊, 没有办法定位问题。

定位问题

因为之前是可以导出的,那么可以断定是我最近加的代码导致了报错,于是将最新的代码逐步删除,查看是否报错,最后定位到以下这段代码有问题。

  /user/login:
    get:
      tags: 
      - user
      summary: 用户登录
      operationId: userLogin
      description: 用户登录
      consumes:
      - application/json
      produces:
      - application/json
      parameters: 
      - in: body
        name: 用户登录
        schema:
          type: object
          required:
          - username
          - password
          properties:
            username:
              type: string
            password:
              type: string
      responses:
        200:
          description: 用户登录
          schema:
            $ref: '#/definitions/LoginResponse'
        400:
          description: bad input parameter

逐步缩小范围,定位出了parameter 这段有问题。

      parameters: 
      - in: body
        name: 用户登录
        schema:
          type: object
          required:
          - username
          - password
          properties:
            username:
              type: string
            password:
              type: string

这段代码非常少, 通过多次尝试,确定出 name: 用户登录 这段代码有问题。 将中文 用户登录改成 userlogin 英文之后问题解决。

问题分析

这应该是swagger的一个bug,通过逐渐删除新增的代码来定位问题的方法虽然低效,但是可行。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容