OpenAPITools / OpenAPITools/openapi-generator
[BUG] [PYTHON] Generated Python code fails to deserialize dictionary objects
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Generated Python code fails to deserialize dictionary objects
openapi-generator version
7.5.0
OpenAPI declaration file content or url
see this repo for complete example and explanation.
Generation Details
Problem is probably caused by the code to deserialize a dict:
@classmethod
def from_dict(cls, obj: Dict) -> Self:
"""Create an instance of User from a dict"""
if obj is None:
return None
# Why don't we just let handle pydantic the deserialization? The generated code completely ignores the relations
# attribute.
#
# This code works for me
# _obj = cls.model_validate(obj)
# return _obj
#
# The generated code below does not
if not isinstance(obj, dict):
return cls.model_validate(obj)
_obj = cls.model_validate({
"name": obj.get("name"),
"address": Address.from_dict(obj.get("address")) if obj.get("address") is not None else None,
})
return _obj
Steps to reproduce
Checkout this repo
pip install -r requirements.txt
uvicorn main:app --reload
python client.py
Related issues/PRs
Suggest a fix
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked reproduction repository, install requirements.txt, run uvicorn main:app --reload, and execute client.py. Inspect the generated Python model's from_dict path and compare its handling of dictionary objects and the relations attribute with the reproduction's expected behavior; done means the client deserializes the example successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100