如何使用引用我自己的 *_controller.py 实现的 OpenAPI 生成器生成 Python Flask 服务器?

How to generate a Python Flask server with OpenAPI Generator that references my own implementation of *_controller.py?

我想生成一个 Python Flask 服务器,提供特定的 OpenAPI 规范作为输入 - 假设 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 的文件以及每个方法在此文件中定义 returns 相同的模板字符串:

'do some magic!'

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之后,仍然不是很清楚我怎样才能做到这一点。

如有任何帮助,我们将不胜感激。

我的问题的解决方案是下载 Swagger Codegen (link),找到 controller.mustache 模板文件 Python- Flask 服务器(位于此处: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

感谢 Dudi for his 类似的问题。