OpenAPITools / OpenAPITools/openapi-generator

[BUG] [PYTHON] Generated Python code fails to deserialize dictionary objects

Open
#18,459 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.