Skip to content

[Python] Fix python template for list and dicts of dicts#23112

Merged
wing328 merged 3 commits intoOpenAPITools:masterfrom
ottmar-zittlau:fix/Python-template-for-dict
Mar 6, 2026
Merged

[Python] Fix python template for list and dicts of dicts#23112
wing328 merged 3 commits intoOpenAPITools:masterfrom
ottmar-zittlau:fix/Python-template-for-dict

Conversation

@ottmar-zittlau
Copy link
Contributor

@ottmar-zittlau ottmar-zittlau commented Mar 4, 2026

I had some issues generating python code for dicts.
I made some changes to the corresponding template to fix these.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes Python generator templates to correctly handle lists of dicts and dicts of dicts, ensuring nested models in arrays/maps serialize to valid JSON and deserialize reliably. Adds spec fixtures, tests, and generated samples to cover arrays of maps and maps of maps.

  • Bug Fixes
    • Lists of dicts (arrays of maps): serialize inner model values with to_dict; deserialize each value with Model.from_dict.
    • Dicts of dicts (maps of maps): serialize nested dict values with to_dict and rebuild them during from_dict.
    • Container branching: add explicit array-of-map and map-of-map paths; use obj["field"] when iterating to avoid None.items() errors.

Written for commit 7c1a8fd. Summary will update on new commits.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/python/model_generic.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/python/model_generic.mustache:375">
P1: `from_dict` crashes when map-of-map field is present but set to null. The condition `"{{{baseName}}}" in obj` only checks key presence, not value. If the input contains the key with explicit null value (valid for nullable properties), `obj["{{{baseName}}}"].items()` will raise `AttributeError: 'NoneType' object has no attribute 'items'`.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@wing328
Copy link
Member

wing328 commented Mar 4, 2026

thanks for the fix

can you please add a test schema to modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml and follow step 3 to regenerate the samples ?

(please run the sample update command twice)

@wing328
Copy link
Member

wing328 commented Mar 4, 2026

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@ottmar-zittlau ottmar-zittlau force-pushed the fix/Python-template-for-dict branch from 41e3152 to f501959 Compare March 4, 2026 17:03
@ottmar-zittlau
Copy link
Contributor Author

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

Thanks - I think I managed to fix it.

@ottmar-zittlau ottmar-zittlau reopened this Mar 4, 2026
@ottmar-zittlau ottmar-zittlau force-pushed the fix/Python-template-for-dict branch from f501959 to 7c1a8fd Compare March 4, 2026 22:20
@ottmar-zittlau
Copy link
Contributor Author

thanks for the fix

can you please add a test schema to modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml and follow step 3 to regenerate the samples ?

(please run the sample update command twice)

@wing328 Thanks for the quick feedback. I added two test schemas - one is a dict of dicts with non-primitive types and the other one is a list of dicts. I also generated and added the samples.

I think that the code generated by pydantic v1 is wrong (cf. https://github.com/OpenAPITools/openapi-generator/pull/23112/changes#diff-fda969d5c6bc4a395c4b0f943f87e8fd63bbe315e680336db016f1ffcec967b0R65) - to_dict is called on the values of a dict of dicts, i.e. on dicts that don't have this property. Should I fix this? And if yes, in this PR or separately?

@wing328
Copy link
Member

wing328 commented Mar 5, 2026

And if yes, in this PR or separately?

with a separate PR please

@wing328
Copy link
Member

wing328 commented Mar 5, 2026

cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) @multani (2023/10)

@wing328 wing328 added this to the 7.21.0 milestone Mar 5, 2026
@wing328 wing328 merged commit 4d83cfe into OpenAPITools:master Mar 6, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants