问题描述
我正在尝试从使用 OpenAPI 2.0 描述的 Python 应用程序处理一个简单的HTML 表单,并使用生成Flask-Connexion 代码OpenAPI Generator v4.3.1 和 v5.0.1,但我总是得到如下响应:
"['whatever'] is not of type 'string'"
这是我的 OpenAPI 2.0 规范,它似乎符合 https://swagger.io/docs/specification/2-0/describing-parameters/ 的“表单参数”部分:
swagger: "2.0"
info:
version: "1.0.0"
title: "Test"
host: localhost:8080
basePath: /api
schemes:
- http
paths:
/mytest:
post:
operationId: process_test_form
consumes:
- application/x-www-form-urlencoded
produces:
- text/html; charset=utf-8
parameters:
- name: myparam
in: formData
description: "my param"
type: string
required: True
responses:
"200":
description: "Success"
schema:
type: string
我使用 OpenAPI Generator 4.3.1 和 v5.0.1 生成 Python 应用程序,如下所示:
java -jar openapi-generator-cli-4.3.1.jar generate -g python-flask -i ./spec.yml
尊重生成的 "requeriments.txt
" 我做了 "pip3 install -r requirements.txt
" ,所以我终于使用了:
Werkzeug (0.16.1)
swagger-ui-bundle (0.0.8)
python-dateutil (2.8.1)
设置工具 (39.0.1)
连接 (2.7.0)
python3 -m openapi_server
可以使用这样的简单 HTML 表单轻松测试:
<html>
<body>
<h1>My test</h1>
<form action="http://0.0.0.0:8080/api/mytest" method="post">
<input type="text" name="myparam"><br/>
<input type="submit" name="Accept"><br/>
</form>
</body>
</html>
但是总是报错:
"['anytextentered'] is not of type 'string'"
我找不到问题所在。 EndPoint 已创建并从表单中正确定位,因为如果我省略参数,我会收到错误“"'myparam' is a required property"
”。也许生成的 requeriments.txt 中缺少某些内容?
任何帮助将不胜感激。 提前致谢! /天使
编辑:我测试了 Software2 提供的 v3.0 规范,它在我的环境中生成了相同的错误(我使用的是“OpenAPI Generator”生成的错误)。一个有效的“requeriments.txt”将不胜感激。
解决方法
Swagger 2.0 已经很老了。虽然它可能是有效的,但使用您的规范的工具之一中可能存在错误。 (即使这不能解决问题,更新到这种更现代的格式可能是个好主意。)尝试使用 OpenAPI 3.0。这是通过自动转换器运行的代码:
openapi: 3.0.1
info:
title: Test
version: 1.0.0
servers:
- url: http://localhost:8080/api
paths:
/mytest:
post:
operationId: process_test_form
requestBody:
content:
application/x-www-form-urlencoded:
schema:
required:
- myparam
properties:
myparam:
type: string
description: my param
required: true
responses:
200:
description: Success
content:
text/html; charset=utf-8:
schema:
type: string
components: {}