OpenAPITools / OpenAPITools/openapi-generator

[BUG][python] python-flask server ImportError exception at launch

Open
#11,855 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generating server code with python-flask and launching the program leads to ImportError exception: ImportError: cannot import name 'json' from 'itsdangerous' (/usr/local/lib/python3.10/site-packages/itsdangerous/__init__.py). This is probably caused by the following:

https://github.com/pallets/flask/issues/4455
https://github.com/pallets/markupsafe/issues/284

Full stack trace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/src/app/test_rest_api/__main__.py", line 5, in <module>
    from test_rest_api import encoder
  File "/usr/src/app/test_rest_api/encoder.py", line 1, in <module>
    from connexion.apps.flask_app import FlaskJSONEncoder
  File "/usr/local/lib/python3.10/site-packages/connexion/apps/flask_app.py", line 11, in <module>
    import flask
  File "/usr/local/lib/python3.10/site-packages/flask/__init__.py", line 19, in <module>
    from . import json
  File "/usr/local/lib/python3.10/site-packages/flask/json/__init__.py", line 15, in <module>
    from itsdangerous import json as _json
ImportError: cannot import name 'json' from 'itsdangerous' (/usr/local/lib/python3.10/site-packages/itsdangerous/__init__.py)
openapi-generator version

Latest Docker image

openapi-generator-cli 6.0.0-SNAPSHOT
  commit : 6f37409
  built  : 2022-03-10T08:57:57Z
OpenAPI declaration file content or url

Reproduced with any specification file. It does not depend on OpenAPI definitions.

Generation Details
docker run --name "openapi-generator-cli" --rm -u $(id -u ${USER}):$(id -g ${USER}) --mount type=bind,src=$(pwd),dst=/host openapitools/openapi-generator-cli:latest generate --input-spec /host/openapi.yaml --generator-name python-flask --config /host/python-flask-config.yaml --output /host/output/python-flask/
Steps to reproduce
docker run --name "openapi-generator-cli" --rm -u $(id -u ${USER}):$(id -g ${USER}) --mount type=bind,src=$(pwd),dst=/host openapitools/openapi-generator-cli:latest generate --input-spec /host/openapi.yaml --generator-name python-flask --config /host/python-flask-config.yaml --output /host/output/python-flask/

cd output/python-flask/

docker build -t test_rest_api .

docker run -p 5000:5000 test_rest_api
Related issues/PRs

None.

Suggest a fix

It can be fixed by changing the Flask version and setting a specific MarkupSafe version as suggested in the relevant issues of above. So manually editing the requirements.txt file from this:

connexion[swagger-ui] >= 2.6.0; python_version>="3.6"
# 2.3 is the last version that supports python 3.4-3.5
connexion[swagger-ui] <= 2.3.0; python_version=="3.5" or python_version=="3.4"
# connexion requires werkzeug but connexion < 2.4.0 does not install werkzeug
# we must peg werkzeug versions below to fix connexion
# https://github.com/zalando/connexion/pull/1044
werkzeug == 0.16.1; python_version=="3.5" or python_version=="3.4"
swagger-ui-bundle >= 0.0.2
python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 1.1.2

to this

connexion[swagger-ui] >= 2.6.0; python_version>="3.6"
# 2.3 is the last version that supports python 3.4-3.5
connexion[swagger-ui] <= 2.3.0; python_version=="3.5" or python_version=="3.4"
# connexion requires werkzeug but connexion < 2.4.0 does not install werkzeug
# we must peg werkzeug versions below to fix connexion
# https://github.com/zalando/connexion/pull/1044
werkzeug == 0.16.1; python_version=="3.5" or python_version=="3.4"
swagger-ui-bundle >= 0.0.2
python_dateutil >= 2.6.0
setuptools >= 21.0.0
Flask == 1.1.4
MarkupSafe == 2.0.1

Fixes the issue.

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 with the python-flask generator's generated requirements.txt, using the reported Docker generation and launch commands to reproduce the ImportError. Compare the listed Flask and MarkupSafe constraints with the versions in the report. Done means a generated python-flask server builds and launches without the itsdangerous import failure.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.