生成python-flask服务器存根时没有定义模型对象

时间:2018-05-01 20:05:01

标签: swagger-codegen openapi

更新:我发现了这个问题。我使用术语"文件"对于定义中的对象似乎导致此问题。我将对象重命名为" FileThing"作为测试和swagger-codegen生成预期的模型对象和模块。 (也许这与https://github.com/swagger-api/swagger-codegen/issues/3223相关?)。

当使用swagger-codegen从OpenAPI v2规范生成Python Flask(Connexion)服务器存根时,我在生成的源代码中看不到任何模型对象。但是,其他语言/ API目标(如go-server)会生成模型对象。我正在使用swagger-codegen-cli-2.3.1.jar。我的规范在这一点上非常简单,并且是:

swagger: "2.0"

info:
  version: 0.0.1
  title: Blah API
  description: An API for interacting with the blah system

paths:

  /files:

    get:
      description: Retrieves information on files that match the specified criteria

      produces:
      - "application/json"

      parameters:
        - name: tags
          type: array
          description: The tags for which matching files are to be returned
          in: query
          items:
            type: string

      responses:
        '200':
          description: Successful response
          schema:
            type: array
            items:
              $ref: "#/definitions/File"

definitions:
  File:
    type: object
    properties:
      file_id:
        type: "string"
        format: "uuid"
      path:
        type: "string"
      tags:
        type: array
        items:
          type: "string"

如您所见,我希望看到为定义部分中定义的File对象创建的模型对象。但是,Source / swagger_server下的模型包仅包含base_model.py模块,除了基本模型类本身之外没有其他定义。我按如下方式运行swagger-codegen-cli:

sknick@sknick:~/Repo/Blah/Server$ java -jar /home/sknick/Misc/swagger-codegen-cli-2.3.1.jar generate -l python-flask -o ./Source -i ./api.yaml
[main] INFO io.swagger.parser.Swagger20Parser - reading from ./api.yaml
[main] WARN io.swagger.codegen.DefaultCodegen - Empty operationId found for path: GET /files. Renamed to auto-generated operationId: filesGET
[main] INFO io.swagger.codegen.DefaultGenerator - Model File not imported due to import mapping
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/controllers/default_controller.py
[main] INFO io.swagger.codegen.DefaultGenerator - File exists. Skipped overwriting /home/sknick/Repo/Blah/Server/./Source/swagger_server/test/test_default_controller.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/README.md
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/setup.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/tox.ini
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/test-requirements.txt
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/requirements.txt
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/git_push.sh
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.gitignore
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.travis.yml
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/Dockerfile
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.dockerignore
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/__main__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/encoder.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/util.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/controllers/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/models/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/models/base_model_.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/test/__init__.py
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/swagger_server/swagger/swagger.yaml
[main] INFO io.swagger.codegen.AbstractGenerator - writing file /home/sknick/Repo/Blah/Server/./Source/.swagger-codegen/VERSION

1 个答案:

答案 0 :(得分:1)

它看起来像是python-flask生成器中的一个错误,它不会生成与某些Java类名匹配的模型,例如FileDateSet等。 "忽略模型"行为在DefaultCodegen课程中通过importMapping在此配置:
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java#L835

某些生成器(如C#)清除importMapping因为它们不使用Java类映射:
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java#L56

importMapping.clear();

但是python-flask生成器不会这样做。它应该也可以清除importMapping,但我不是Python / Flask dev,所以我不确定。

我建议您在Swagger Codegen存储库中open an issue,和/或提交PR。

与此同时,一些可能的解决方法是:

  • 重命名模型,例如到FileDTO
  • Create a custom generator基于python-flask,添加importMapping.clear(),并使用您的自定义生成器。