swagger-api / swagger-api/swagger-codegen

[python] bug cant run vanilla shipped Petstore python-flask generated code

Open
#12,278 6 comments 8 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 default python-server generated code does not work out of the box.
When running the code against a fresh install environment, the server cannot be started, instead we get "ModuleNotFoundError: No module named 'connexion.apps.flask_app'". This happens to the default Petstore example.

Swagger-codegen version

default Petstore OAS 3.0 example from swagger editor website (see below)

Swagger declaration file content or url

default Petstore OAS 3.0 example from swagger editor website (see below)

Command line used for generation

generation through website

Steps to reproduce
> open swagger editor (https://editor.swagger.io/)
> Edit > Load pet store 3.0 OAS
> Generate Server
> python-flask (downlods python repository python-flask-server-generated.zip)
> extract zip file to ~/server
> create environment 
> conda create -n server python=3.11
> conda activate server
> pip install -r requirements.txt
> conda list

#
# Name                    Version                   Build  Channel
anyio                     4.0.0                    pypi_0    pypi
asgiref                   3.7.2                    pypi_0    pypi
attrs                     23.1.0                   pypi_0    pypi
bzip2                     1.0.8                he774522_0    defaults
certifi                   2023.11.17               pypi_0    pypi
charset-normalizer        3.3.2                    pypi_0    pypi
connexion                 3.0.1                    pypi_0    pypi
h11                       0.14.0                   pypi_0    pypi
httpcore                  1.0.2                    pypi_0    pypi
httpx                     0.25.1                   pypi_0    pypi
idna                      3.4                      pypi_0    pypi
inflection                0.5.1                    pypi_0    pypi
jinja2                    3.1.2                    pypi_0    pypi
jsonschema                4.20.0                   pypi_0    pypi
jsonschema-specifications 2023.11.1                pypi_0    pypi
libffi                    3.4.4                hd77b12b_0    defaults
markupsafe                2.1.3                    pypi_0    pypi
openssl                   3.0.12               h2bbff1b_0    defaults
pip                       23.3            py311haa95532_0    defaults
python                    3.11.5               he1021f5_0    defaults
python-dateutil           2.6.0                    pypi_0    pypi
python-multipart          0.0.6                    pypi_0    pypi
pyyaml                    6.0.1                    pypi_0    pypi
referencing               0.31.0                   pypi_0    pypi
requests                  2.31.0                   pypi_0    pypi
rpds-py                   0.13.0                   pypi_0    pypi
setuptools                68.0.0          py311haa95532_0    defaults
six                       1.16.0                   pypi_0    pypi
sniffio                   1.3.0                    pypi_0    pypi
sqlite                    3.41.2               h2bbff1b_0    defaults
starlette                 0.32.0.post1             pypi_0    pypi
swagger-ui-bundle         1.1.0                    pypi_0    pypi
tk                        8.6.12               h2bbff1b_0    defaults
typing-extensions         4.8.0                    pypi_0    pypi
tzdata                    2023c                h04d1e81_0    defaults
urllib3                   2.1.0                    pypi_0    pypi
vc                        14.2                 h21ff451_1    defaults
vs2015_runtime            14.27.29016          h5e58377_2    defaults
werkzeug                  3.0.1                    pypi_0    pypi
wheel                     0.41.2          py311haa95532_0    defaults
xz                        5.4.2                h8cc25b3_0    defaults
zlib                      1.2.13               h8cc25b3_0    defaults```

> python -m swagger_server

(server) C:\Users\myuser\Downloads\python-flask-server-generated>python -m swagger_server
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\myuser\Downloads\python-flask-server-generated\swagger_server\__main__.py", line 5, in <module>
    from swagger_server import encoder
  File "C:\Users\myuser\Downloads\python-flask-server-generated\swagger_server\encoder.py", line 1, in <module>
    from connexion.apps.flask_app import FlaskJSONEncoder
ModuleNotFoundError: No module named 'connexion.apps.flask_app'
Related issues/PRs

I checked through search but didnt find any related issues/PR.

Suggest a fix/enhancement

It seems there are breaking changes in connexion from 2 to 3, which are currently not considered by python-flask server generation.
Simple workaround is making sure to use version 2 instead of version 3 in the requirements.txt

requirements.txt, I think this here: https://github.com/swagger-api/swagger-codegen/blob/953805cb9eb750204c965a54ae326c4f308a68d2/modules/swagger-codegen/src/main/resources/flaskConnexion/requirements.mustache#L1

from:
connexion >= 2.6.0
connexion[swagger-ui] >= 2.6.0
to:
connexion>=2, <3
connexion[swagger-ui]>=2, <3

fixes the issue for me.

Here with python 3.9 and fixed requirements.txt

cat requirements.txt
connexion>=2, <3
connexion[swagger-ui]>=2, <3
python_dateutil == 2.6.0
setuptools >= 21.0.0
swagger-ui-bundle >= 0.0.2


(swagger39) C:\Users\myuser\Downloads\python-flask-server-generated>conda list
# packages in environment at C:\Users\myuser\.conda\envs\swagger39:
#
# Name                    Version                   Build  Channel
anyio                     4.0.0                    pypi_0    pypi
asgiref                   3.7.2                    pypi_0    pypi
attrs                     23.1.0                   pypi_0    pypi
certifi                   2023.11.17               pypi_0    pypi
charset-normalizer        3.3.2                    pypi_0    pypi
click                     8.1.7                    pypi_0    pypi
clickclick                20.10.2                  pypi_0    pypi
colorama                  0.4.6                    pypi_0    pypi
connexion                 2.14.2                   pypi_0    pypi
exceptiongroup            1.1.3                    pypi_0    pypi
flask                     2.2.5                    pypi_0    pypi
h11                       0.14.0                   pypi_0    pypi
httpcore                  1.0.2                    pypi_0    pypi
httpx                     0.25.1                   pypi_0    pypi
idna                      3.4                      pypi_0    pypi
importlib-metadata        6.8.0                    pypi_0    pypi
inflection                0.5.1                    pypi_0    pypi
itsdangerous              2.1.2                    pypi_0    pypi
jinja2                    3.1.2                    pypi_0    pypi
jsonschema                4.20.0                   pypi_0    pypi
jsonschema-specifications 2023.11.1                pypi_0    pypi
markupsafe                2.1.3                    pypi_0    pypi
openssl                   3.0.12               h2bbff1b_0    defaults
packaging                 23.2                     pypi_0    pypi
pip                       23.3             py39haa95532_0    defaults
python                    3.9.18               h1aa4202_0    defaults
python-dateutil           2.6.0                    pypi_0    pypi
python-multipart          0.0.6                    pypi_0    pypi
pyyaml                    6.0.1                    pypi_0    pypi
referencing               0.31.0                   pypi_0    pypi
requests                  2.31.0                   pypi_0    pypi
rpds-py                   0.13.0                   pypi_0    pypi
setuptools                68.0.0           py39haa95532_0    defaults
six                       1.16.0                   pypi_0    pypi
sniffio                   1.3.0                    pypi_0    pypi
sqlite                    3.41.2               h2bbff1b_0    defaults
starlette                 0.32.0.post1             pypi_0    pypi
swagger-ui-bundle         0.0.9                    pypi_0    pypi
typing-extensions         4.8.0                    pypi_0    pypi
tzdata                    2023c                h04d1e81_0    defaults
urllib3                   2.1.0                    pypi_0    pypi
vc                        14.2                 h21ff451_1    defaults
vs2015_runtime            14.27.29016          h5e58377_2    defaults
werkzeug                  2.2.3                    pypi_0    pypi
wheel                     0.41.2           py39haa95532_0    defaults
zipp                      3.17.0                   pypi_0    pypi

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 modules/swagger-codegen/src/main/resources/flaskConnexion/requirements.mustache and compare its Connexion dependency with the generated encoder.py import. Regenerate the default Petstore python-flask server, install its requirements, and run python -m swagger_server; done means the shipped server starts without the reported ModuleNotFoundError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.