无法从 OpenAPI 2.0 生成的 Python-Flask 应用程序中获取表单参数



我正在尝试处理一个简单的HTML表单从使用OpenAPI 2.0描述的Python应用程序并生成Flask-Connexion使用OpenAPI Generator v4.3.1的代码和v5.0.1,但我总是得到这样的回复:

"['whatever'] is not of type 'string'"

这是我的OpenAPI 2.0规范,它似乎适合">表单参数"部分。Of 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生成Python应用程序和v5.0.1这样的:

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发布)setuptools (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'"

我找不到哪里不对。创建了端点,并且它从表单中正确地定位,因为如果我省略了参数,我会得到一个错误""'myparam' is a required property""。也许在生成的requirements .txt中缺少了什么?

任何帮助都将非常感激。提前感谢!/天使

编辑:我测试了Software2给出的v3.0规范,它在我的环境中产生了同样的错误(我使用的是由OpenAPI Generator生成的)。工作"需求"。非常感谢。

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: {}

我知道已经一年了,但我刚刚遇到你的问题,我自己也有同样的问题。我设法发现,这是一个缺失的type:object在顶部,似乎是保持它的工作通过查看这里的例子。

requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object  <-- this was what I was missing
properties:
id:
type: string
format: uuid

我希望这至少对你来说是有趣的,即使它可能太迟了。

丹尼尔

最新更新