2

我想生成一个 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的文件,并且此文件中定义的每个方法都返回相同的模板字符串:

“做点魔术!”

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之后,我仍然不太清楚如何实现这一点。

任何帮助将不胜感激。

4

1 回答 1

3

我的问题的解决方案是下载 Swagger Codegen(链接),找到 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

感谢 Dudi对类似问题的回答。

于 2019-02-18T15:32:53.707 回答