如何解决如何解决从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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。