swagger-api / swagger-api/swagger-codegen

[python-flask] Generated code fails due to .from_dict syntax error.

Open
#10,521 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The python-flask generated server doesn't run due syntax errors:

  File "/usr/src/app/swagger_server/controllers/default_controller.py", line 20
    filter = .from_dict(connexion.request.get_json())  # noqa: E501
Swagger-codegen version

3.0.22

Swagger declaration file content or url

Tested against the PetStore V 3.0 with an anyOf element, eg https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml with

          schema:
            oneOf:
            - type: string
            - type: integer

Or the below complete example:

openapi: "3.0.0"
info:
  title: "My API"
  version: "0.0.1"
paths:
  /invoices:
    get:
      parameters:
        - name: filter
          in: query
          description: Filter parameters
          required: false
          schema:
            type: object
            properties:
              field:
                type: string
      responses:
        '200':
          description: An array of invoices
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    number:
                      type: integer
                      format: int64
Command line used for generation
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli-v3:3.0.22  generate -i /local/swagger.yaml -l python-flask -o /local

19:14:45.328 [Thread-1] WARN  i.s.c.v.g.DefaultCodegenConfig - Empty operationId found for path: GET /invoices. Renamed to auto-generated operationId: invoicesGET
19:14:45.757 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/models/filter.py
19:14:45.811 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/models/inline_response200.py
19:14:45.930 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/controllers/default_controller.py
19:14:45.937 [Thread-1] INFO  i.s.codegen.v3.DefaultGenerator - File exists. Skipped overwriting /local/swagger_server/test/test_default_controller.py
19:14:46.099 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/README.md
19:14:46.107 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/setup.py
19:14:46.114 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/tox.ini
19:14:46.120 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/test-requirements.txt
19:14:46.129 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/requirements.txt
19:14:46.141 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/git_push.sh
19:14:46.153 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/.gitignore
19:14:46.163 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/.travis.yml
19:14:46.172 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/Dockerfile
19:14:46.184 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/.dockerignore
19:14:46.194 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/__init__.py
19:14:46.203 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/__main__.py
19:14:46.214 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/encoder.py
19:14:46.233 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/util.py
19:14:46.242 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/controllers/__init__.py
19:14:46.250 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/models/__init__.py
19:14:46.264 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/models/base_model_.py
19:14:46.275 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/test/__init__.py
19:14:46.286 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/swagger/swagger.yaml
connexion == 2.6.0
19:14:46.297 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/swagger_server/controllers/authorization_controller.py
19:14:46.302 [Thread-1] INFO  i.s.codegen.v3.AbstractGenerator - writing file /local/.swagger-codegen/VERSION
Steps to reproduce

generate using above command.
Edit the requirements.txt to workaround existing bug https://github.com/swagger-api/swagger-codegen-generators/issues/780
docker build -t testcase .
docker run testcase

Thanks for any assistance, we'd love to use the project.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the documented Docker generation command with the supplied OpenAPI example, then inspect the generated swagger_server/controllers/default_controller.py and the related generated model files. Confirm that the generated server currently contains the invalid .from_dict expression; done means the same input produces syntactically valid Python that starts successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, openapi, python
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.