我想生成一个提供某些OpenAPI规范作为输入的Python Flask服务器-假设foo.yaml-运行以下命令:
java -jar openapi-generator-cli.jar generate -i foo.yaml -g python-flask -o python-flask_api_server
但是,这会生成一个服务器存根,其中包含一个位于 \ python-flask_api_server \ openapi_server \ controllers 下的名为 foo_controller.py 的文件,并且此文件中定义的每个方法都返回相同的内容模板字符串:
'做一些魔术!'
foo_controller.py
def foo_post(inline_object=None): # noqa: E501
"""Create a foo
# noqa: E501
:param inline_object:
:type inline_object: dict | bytes
:rtype: str
"""
if connexion.request.is_json:
inline_object = InlineObject.from_dict(connexion.request.get_json()) # noqa: E501
return 'do some magic!'
我要使用OpenAPI Generator进行的操作是生成一个服务器存根,其 foo_controller.py 引用我自己对此文件的实现,例如:
foo_controller.py (生成的文件)
import foo_controller_impl
def foo_post(inline_object=None): # noqa: E501
"""Create a foo
# noqa: E501
:param inline_object:
:type inline_object: dict | bytes
:rtype: str
"""
foo_controller_impl.foo_post_impl(inline_object)
foo_controller_impl.py (我对foo_controller.py的实现)
def foo_post_impl(inline_object=None): # noqa: E501
if connexion.request.is_json:
inline_object = InlineObject.from_dict(connexion.request.get_json()) # noqa: E501
print("Request body is:\n" + str(inline_object))
response = "/foo/1"
return response
我运行以下命令来生成新的模板集:
java -jar openapi-generator-cli.jar meta -o my-codegen -n myCodegen -p org.openapitools.codegen
但是在阅读生成的 README.md 并检查了 MycodegenGenerator.java 之后,我仍然不清楚如何实现这一目标。
任何帮助将不胜感激。
答案 0 :(得分:1)
我的问题的解决方案是下载Swagger Codegen(link),找到适用于Python-Flask服务器的 controller.mustache 模板文件(位于此处: swagger- codegen-master \ modules \ swagger-codegen \ src \ main \ resources \ flaskConnexion )并按如下所示进行编辑:
from {{packageName}}.controllers import {{classname}}_impl
{{#operations}}
{{#operation}}
def {{operationId}}({{#allParams}}{{paramName}}{{^required}}=None{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}): # noqa: E501
"""{{#summary}}{{.}}{{/summary}}{{^summary}}{{operationId}}{{/summary}}
{{#notes}}{{.}}{{/notes}} # noqa: E501
{{#allParams}}
:param {{paramName}}: {{description}}
{{^isContainer}}
{{#isPrimitiveType}}
:type {{paramName}}: {{>param_type}}
{{/isPrimitiveType}}
{{#isUuid}}
:type {{paramName}}: {{>param_type}}
{{/isUuid}}
{{^isPrimitiveType}}
{{#isFile}}
:type {{paramName}}: werkzeug.datastructures.FileStorage
{{/isFile}}
{{^isFile}}
{{^isUuid}}
:type {{paramName}}: dict | bytes
{{/isUuid}}
{{/isFile}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isListContainer}}
{{#items}}
{{#isPrimitiveType}}
:type {{paramName}}: List[{{>param_type}}]
{{/isPrimitiveType}}
{{^isPrimitiveType}}
:type {{paramName}}: list | bytes
{{/isPrimitiveType}}
{{/items}}
{{/isListContainer}}
{{#isMapContainer}}
{{#items}}
{{#isPrimitiveType}}
:type {{paramName}}: Dict[str, {{>param_type}}]
{{/isPrimitiveType}}
{{^isPrimitiveType}}
:type {{paramName}}: dict | bytes
{{/isPrimitiveType}}
{{/items}}
{{/isMapContainer}}
{{/allParams}}
:rtype: {{#returnType}}{{.}}{{/returnType}}{{^returnType}}None{{/returnType}}
"""
return {{classname}}_impl.{{operationId}}({{#allParams}}{{paramName}}{{^required}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{{/operation}}
{{/operations}}
最后,我使用以下命令来生成Python-Flask服务器:
java -jar swagger-codegen-cli-2.3.1.jar generate -i foo.yaml -l python-flask -o "swagger server\foo" -t swagger-codegen-master\modules\swagger-codegen\src\main\resources\flaskConnexion