swagger-api / swagger-api/swagger-codegen

[PYTHON] Generated python server cannot import controller

Open
#8,747 0 comments 3 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

Some generated controller names are inconsistent between usage in import statements, and the name of the file.

Swagger-codegen version

2.4.0-SNAPSHOT

Swagger declaration file content or url
swagger: '2.0'
info:
  title: bad tag
  description: blah
  version: '0.0'
tags:
  - name: ABCD 1234
    description: blah
paths:
  /something:
    get:
      tags:
        - ABCD 1234
      responses:
        '200':
          description: Successful
Command line used for generation
java -jar swagger-codegen-cli.jar generate \
  -i tag.yaml \
  -l python-flask \
  -o tag/ \
  -s
Steps to reproduce

After running code generation, attempt to start the python server: python3 -m swagger_server. This throws an exception.

Related issues/PRs

This seems similar to issue #7357.

Suggest a fix/enhancement

It seems there is inconsistent name mangling. The module being imported is named abcd_1234_controller, whereas the file is named abcd1234_controller.py.

Python server output
$ python3 -m swagger_server
Failed to add operation for GET /something
Traceback (most recent call last):
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apis/abstract.py", line 242, in add_paths
    self.add_operation(method, path, endpoint, path_parameters)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apis/abstract.py", line 195, in add_operation
    pythonic_params=self.pythonic_params)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/operation.py", line 215, in __init__
    resolution = resolver.resolve(self)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/resolver.py", line 40, in resolve
    return Resolution(self.resolve_function_from_operation_id(operation_id), operation_id)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/resolver.py", line 65, in resolve_function_from_operation_id
    raise ResolverError(msg, sys.exc_info())
connexion.exceptions.ResolverError: <ResolverError: Cannot resolve operationId "swagger_server.controllers.abcd_1234_controller.something_get"! Import error was "No module named 'swagger_server.controllers.abcd_1234_controller'">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/ethan/Dev/bad-swagger/tag/swagger_server/__main__.py", line 16, in <module>
    main()
  File "/home/ethan/Dev/bad-swagger/tag/swagger_server/__main__.py", line 11, in main
    app.add_api('swagger.yaml', arguments={'title': 'bad tag'})
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apps/flask_app.py", line 54, in add_api
    api = super(FlaskApp, self).add_api(specification, **kwargs)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apps/abstract.py", line 159, in add_api
    options=api_options.as_dict())
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apis/abstract.py", line 131, in __init__
    self.add_paths()
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apis/abstract.py", line 249, in add_paths
    self._handle_add_operation_error(path, method, err.exc_info)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/apis/abstract.py", line 263, in _handle_add_operation_error
    six.reraise(*exc_info)
  File "/home/ethan/.local/lib/python3.5/site-packages/six.py", line 693, in reraise
    raise value
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/resolver.py", line 62, in resolve_function_from_operation_id
    return self.function_resolver(operation_id)
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/utils.py", line 46, in get_function_from_name
    raise last_import_error
  File "/home/ethan/.local/lib/python3.5/site-packages/connexion/utils.py", line 34, in get_function_from_name
    module = importlib.import_module(module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'swagger_server.controllers.abcd_1234_controller'

Filesystem:

$ ls swagger_server/controllers
abcd1234_controller.py  __init__.py  __pycache__

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 generation with the supplied Swagger YAML and the python-flask command, then inspect the generated swagger_server/controllers filenames and the operation imports used when running python3 -m swagger_server. Done means the controller module name and filename use consistent mangling so the generated server starts without the reported import error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.