我有一个简单的 API,它有一个需要三个参数的后端点。当我通过 python-fask codegen SwaggerUI 对其进行测试时,我收到错误消息,指出没有足够的值来解包。 server-stub 是从 swagger-hub 自动生成的,预计可以工作 :) 如何让它工作?详情如下:
来自swagger yaml文件:
paths:
/estimate:
post:
tags:
- client_requests
summary: submits a job by specifying a script file
operationId: e_post
requestBody:
content:
application_json: #WRONG should be application/json as I later discovered.
schema:
$ref: '#/components/schemas/EJob'
responses:
"201":
description: job_received
"400":
description: invalid_input
callbacks:
statusUpdate:
'{$request.body#/callback_url}':
post:
requestBody:
content:
application_json:
schema:
$ref: '#/components/schemas/EJobStatus'
required: true
responses:
"200":
description: Accepted by the server
x-openapi-router-controller: swagger_server.controllers.client_requests_controller
和来自 yaml 的 Ejob Schema:
schemas:
EJob:
required:
- callback_url
- id
- script_url
type: object
properties:
id:
type: integer
format: int64
example: 39
script_url:
type: string
description: A job for backend to execute
format: uri
example: file:///data/39_script.json
callback_url:
type: string
description: This URL is called to update status
format: uri
example: http:://192.168.100.30:50/update_status
来自 Swagger UI 的 post 请求:
curl -X 'POST' \
'http://localhost:8080/BLA/BLA/1.0.0/estimate' \
-H 'accept: */*' \
-H 'Content-Type: application_json' \
-d '{
"callback_url": "http://192.168.100.30:50/update_status",
"id": 39,
"script_url": "file:///data/39_script.json"
}'
我从 swaggerhub python-flask codegen 生成的代码中得到的错误:
[2023-04-26 17:21:43,069] ERROR in app: Exception on /BLA/BLA/1.0.0/estimate [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/decorator.py", line 68, in wrapper
response = function(request)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/uri_parsing.py", line 149, in wrapper
response = function(request)
File "/usr/local/lib/python3.6/site-packages/connexion/decorators/validation.py", line 148, in wrapper
if all_json(self.consumes):
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 165, in all_json
return all(is_json_mimetype(mimetype) for mimetype in mimetypes)
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 165, in <genexpr>
return all(is_json_mimetype(mimetype) for mimetype in mimetypes)
File "/usr/local/lib/python3.6/site-packages/connexion/utils.py", line 139, in is_json_mimetype
maintype, subtype = mimetype.split('/') # type: str, str
ValueError: not enough values to unpack (expected 2, got 1)
172.17.0.1 - - [26/Apr/2023 17:21:43] "POST /BLA/BLA/1.0.0/estimate HTTP/1.1" 500
post requestBody 内容类型应该是application/json 而不是application_json。理想情况下,swaggerhub 上的编辑应该将此标记为错误。