OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] Model template doesn't properly handle nested dicts and arrays.

Open
#22,211 2 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I have some components that contain dicts of dicts. With the default templates the generator doesn't generate valid python-code for these components.

Here is the generated code: https://gist.github.com/ottmar-zittlau/9d2d98ffd9b29c3bcb34b9e947228354

Some bugs:

Here the class on which from_dict is called is a type.

"array_of_dictionaries": [Dict[str, Vector3d].from_dict(_item) for _item in obj["array_of_dictionaries"]] if obj.get("array_of_dictionaries") is not None else None,

Here _item_array_of_dictionaries is a dictionary that doesn't have a to_dict method.

        if self.array_of_dictionaries:
            for _item_array_of_dictionaries in self.array_of_dictionaries:
                if _item_array_of_dictionaries:
                    _items.append(_item_array_of_dictionaries.to_dict())
            _dict['array_of_dictionaries'] = _items
openapi-generator version

7.16.0

OpenAPI declaration file content or url

https://gist.github.com/ottmar-zittlau/2e2a5357b1d1e60d41a2aef4ee6bde14

Generation Details

I generated code for python.

Steps to reproduce

Run

./openapi-generator-cli generate --generator-name python --input-spec openapi.yaml --output generated --package-name example
Related issues/PRs
Suggest a fix

I already have adapted the model_generic.mustache template file to deal with this, but before opening a merge request I wanted to know if this bug is known and whether there is already work going on to fix it.

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 Python generator's model_generic.mustache template and the OpenAPI declaration linked in the issue. Run the provided openapi-generator-cli command to reproduce the generated code shown in the gist, then verify that nested dictionaries and arrays produce valid Python conversion code without calling from_dict or to_dict on plain dictionaries.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.