如何解决从openapi go代码生成器获取使用应用程序/ x-www-form-urlencoded内容类型的API的验证错误?

问题描述

我已遵循提及here的开放式api指南定义了一个API,该API消耗了application / x-www-form-urlencoded并写在API之下:

{
    "openapi": "3.0.0","info": {
        "version": "1.0.draft","title": "user management api","description": "This document defines interface to user management"
    },"servers": [{
        "url": "{apiRoot}/test","variables": {
            "apiRoot": {
                "default": "https://example.com"
            }
        }
    }],"paths": {
        "/users/resetpassword": {
            "post": {
                "summary": "reset password of a user","operationId": "resetUserPassword","parameters": [{
                        "name": "username","in": "formData","required": true,"schema": {
                            "type": "string"
                        }
                    },{
                        "name": "password","schema": {
                            "type": "string"
                        }
                    }
                ],"responses": {
                    "204": {
                        "description": "User password has been changed"
                    }
                }
            }
        }
    }
}

使用以下命令从上方打开的API文档使用code generator生成go代码:

docker run --rm -v ${PWD}:<file_path> openapitools/openapi-generator-cli generate -i <file_path> --skip-validate-spec -g go-server -o <file_out_path>

出现以下错误:

-attribute paths.'/users/resetpassword'(post).parameters.[username].in is not of type `string`
-attribute paths.'/users/resetpassword'(post).parameters.[password].in is not of type `string`

如何解决以上错误?

解决方法

您混合使用了OpenAPI 2.0和OpenAPI 3.0语法。

在OpenAPI 3.0中,使用requestBody关键字定义请求正文(包括表单数据)。将parameters部分替换为:

"requestBody": {
  "required": true,"content": {
    "application/x-www-form-urlencoded": {
      "schema": {
        "type": "object","required": [
          "username","password"
        ],"properties": {
          "username": {
            "type": "string"
          },"password": {
            "type": "string"
          }
        }
      }
    }
  }
}

YAML版本:

requestBody:
  required: true
  content:
    application/x-www-form-urlencoded:
      schema:
        type: object
        required:
        - username
        - password
        properties:
          username:
            type: string
          password:
            type: string

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...