OpenAPI/Swagger+Connexion如何将请求JSON传入operationId对应函数
我尝试使用OpenAPI/Swagger搭建一个可接收JSON字典的接口,将接收到的参数传入指定函数并获取响应。
我使用operationId指向要调用的函数,但无法确定如何将接口接收到的字典参数传递给该函数。
当前配置下controllers.get_options可被正常调用,但没有接收到任何传入参数,应该是遗漏了某个明显的配置项,但暂时没有发现。
我调用接口的方式如下:
curl -X 'POST' \ 'http://localhost:8080/getoptions' \ -H 'accept: */*' \ -H 'Content-Type: application/json' \ -d '{ "product_area": "Main", "product_type": "New" }'
以下是openapi配置文件(./openapi.yaml):
编辑补充:根据Helen的评论,添加
x-body-name: DiscussionResult后问题已解决
openapi: 3.0.0 info: title: Test version: '1.0' paths: /getoptions: post: description: Return product options from product type and area operationId: controllers.get_options requestBody: required: true content: application/json: x-body-name: DiscussionResult schema: $ref: '#/components/schemas/DiscussionResult' responses: 200: description: "success" components: schemas: DiscussionResult: type: object discriminator: propertyName: product_type properties: product_type: type: string example: "New" product_area: type: string example: "Main"
我使用connexion运行服务,代码如下:main.py
import connexion import logging def create_app(): logging.basicConfig(level=logging.DEBUG) connex_app = connexion.FlaskApp(__name__, specification_dir="./openapi/") connex_app.add_api("./openapi.yaml", resolver_error=501) return connex_app if __name__ == "__main__": app = create_app() app.run(host="0.0.0.0", port=8080)
requirements.txt
connexion[swagger-ui] connexion>=2.2.0 python-jose[cryptography] six>=1.9 Flask>=0.10.1 sqlathanor
以下是要调用的目标函数代码:
def get_options(DiscussionResult): msg = "{} {}".format(DiscussionResult['product_area'], DiscussionResult['product_type']) return jsonify(message=msg), 200
内容的提问来源于stack exchange,提问作者yahop19531
相关产品推荐
相关产品推荐

