Search code examples
swagger-codegenopenapi

No model objects defined when generating python-flask server stub


Update: I discovered the issue. My use of the term "File" for an object in definitions appeared to be causing this issue. I renamed the object to "FileThing" as a test and swagger-codegen generated the expected model object and module. (Perhaps this is related to https://github.com/swagger-api/swagger-codegen/issues/3223 ?).

When using swagger-codegen to generate a Python Flask (Connexion) server stub from an OpenAPI v2 specification, I do not see any model objects in the resulting source code. However, other language/API targets such as go-server do generate model objects. I am using swagger-codegen-cli-2.3.1.jar. My specification is very simple at this point and is:

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"

As you can see, I would expect to see a model object created for the File object defined in the definitions section. However, the models package under Source/swagger_server only contains a base_model.py module with no additional definition beyond the base model class itself. I run swagger-codegen-cli as follows:

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

Solution

  • It looks like a bug in the python-flask generator where it does not generate models that match certain Java class names, such as File, Date, Set, etc. The "ignore model" behavior is configured in the DefaultCodegen class via importMapping here:
    https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java#L835

    Some generators, such as C#, clear the importMapping because they don't use Java class mappings:
    https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java#L56

    importMapping.clear();
    

    but python-flask generator does not do this. It should probably clear importMapping as well, but I'm not a Python/Flask dev so I'm not sure.

    I suggest that you open an issue in the Swagger Codegen repository, and/or submit a PR.

    In the meantime, some possible workarounds are:

    • Rename the model, e.g. to FileDTO.
    • Create a custom generator based on python-flask, add importMapping.clear(), and use your custom generator.