You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When generating a python client using the current release of the openapi generator, the discriminator class imports are wrong.
(To get to this point, I already fixed the circular import by moving the petstore.models.pet import Pet line to the top of the import block in __init__.py files in the apiclient and models packages. (Can be automated by isort's force_to_top option. And is therefore currently fixable in an automated way. So not perfect but fixable for now))
pytest results in:
api-clients/testing/test/test_cat.py:None (api-clients/testing/test/test_cat.py)
ImportError while importing test module '***\api-clients\testing\test\test_cat.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
***\pypoetry\Cache\virtualenvs\petstore-NEbAbLrb-py3.11\Lib\site-packages\_pytest\python.py:617: in _importtestmodule
mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
***\pypoetry\Cache\virtualenvs\petstore-NEbAbLrb-py3.11\Lib\site-packages\_pytest\pathlib.py:567: in import_path
importlib.import_module(module_name)
***\Lib\importlib\__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1206: in _gcd_import
???
<frozen importlib._bootstrap>:1178: in _find_and_load
???
<frozen importlib._bootstrap>:1149: in _find_and_load_unlocked
???
<frozen importlib._bootstrap>:690: in _load_unlocked
???
***\Cache\virtualenvs\petstore-NEbAbLrb-py3.11\Lib\site-packages\_pytest\assertion\rewrite.py:178: in exec_module
exec(co, module.__dict__)
test_cat.py:18: in <module>
from petstore.models.cat import Cat # noqa: E501
..\petstore\__init__.py:20: in <module>
from petstore.api.default_api import DefaultApi
..\petstore\api\__init__.py:4: in <module>
from petstore.api.default_api import DefaultApi
..\petstore\api\default_api.py:22: in <module>
from petstore.models.catkiller import Catkiller
..\petstore\models\__init__.py:17: in <module>
from petstore.models.pet import Pet
..\petstore\models\pet.py:106: in <module>
from petstore.models.dog import dog
E ModuleNotFoundError: No module named 'petstore.models.dog'
This is because the generated file for Pet looks like this (shortened):
# shortened header for bug ticketclassPet(BaseModel):
""" Pet """pet_type: StrictStr=Field(alias="petType")
__properties: ClassVar[List[str]] = ["petType"]
model_config= {
"populate_by_name": True,
"validate_assignment": True
}
# JSON field name that stores the object type__discriminator_property_name: ClassVar[List[str]] ='petType'# discriminator mappings__discriminator_value_class_map: ClassVar[Dict[str, str]] = {
'dog': 'Catkiller','Cat': 'Cat','Lizard': 'Lizard'# <--- here the mapping states that dog should be mapped to Catkiller
}
# shortened class for bug ticketfrompetstore.models.catimportCatfrompetstore.models.dogimportdog# <--- this line is the problem. It should import Catkiller instead of Dog herefrompetstore.models.lizardimportLizardfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:
# TODO: pydantic v2# Pet.model_rebuild()pass
Bug Report Checklist
Description
When generating a python client using the current release of the openapi generator, the discriminator class imports are wrong.
(To get to this point, I already fixed the circular import by moving the
petstore.models.pet import Pet
line to the top of the import block in__init__.py
files in the apiclient and models packages. (Can be automated by isort's force_to_top option. And is therefore currently fixable in an automated way. So not perfect but fixable for now))pytest results in:
This is because the generated file for Pet looks like this (shortened):
The problem line at the bottom should be:
After that all tests are successful
openapi-generator version
OpenAPI declaration file content
Generation Details
Steps to reproduce
poetry install
__init__.py
filespytest .
Related issues/PRs
I could not find one.
The text was updated successfully, but these errors were encountered: