swagger-api / swagger-api/swagger-codegen

[Python] NameError: name 'Category' is not defined from the generated flask server

Open
#10,193 5 comments 18 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

When I run flask-server, I get bellow ImportError.

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/local/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/src/app/swagger_server/__main__.py", line 5, in <module>
    from swagger_server import encoder
  File "/usr/src/app/swagger_server/encoder.py", line 4, in <module>
    from swagger_server.models.base_model_ import Model
  File "/usr/src/app/swagger_server/models/__init__.py", line 9, in <module>
    from swagger_server.models.pet import Pet
  File "/usr/src/app/swagger_server/models/pet.py", line 12, in <module>
    class Pet(Model):
  File "/usr/src/app/swagger_server/models/pet.py", line 18, in Pet
    def __init__(self, id: int=None, category: Category=None, name: str=None, photo_urls: List[str]=None, tags: List[Tag]=None, status: str=None):  # noqa: E501
NameError: name 'Category' is not defined
Swagger-codegen version

2.4.13

Swagger declaration file content or url

https://petstore.swagger.io/v2/swagger.json

Command line used for generation

I generate from swagger-editor.

Steps to reproduce
docker build -t swagger_server .
docker run -p 8080:8080 swagger_server
Related issues/PRs

nothing

Suggest a fix/enhancement

I can fix this bug when adding two line in ./swagger_server/models/pet.py .

  from swagger_server.models.base_model_ import Model
+ from swagger_server.models.category import Category
+ from swagger_server.models.tag import Tag
  from swagger_server import util

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

Reproduce the problem with the provided Docker commands and inspect the generated swagger_server/models/pet.py, especially its imports. Confirm that Category and Tag are available when the generated Flask server starts, then verify the same container startup path completes without the NameError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.