Skip to content

Use Pydantic for json serialization in .to_json method#22111

Closed
EspenHa wants to merge 2 commits intoOpenAPITools:masterfrom
RagnaRockGeo:fix_python_to_json_with_uuid
Closed

Use Pydantic for json serialization in .to_json method#22111
EspenHa wants to merge 2 commits intoOpenAPITools:masterfrom
RagnaRockGeo:fix_python_to_json_with_uuid

Conversation

@EspenHa
Copy link
Copy Markdown
Contributor

@EspenHa EspenHa commented Oct 12, 2025

This PR changes the to_json method of generated models to use
Pydantic's to_jsonable_python, to ensure that the input to json.dumps only contains serializable types.
This fixes #22063.

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.

@EspenHa EspenHa force-pushed the fix_python_to_json_with_uuid branch from 2f29196 to 4863b63 Compare October 12, 2025 09:49
@EspenHa EspenHa marked this pull request as draft October 12, 2025 10:26
@EspenHa
Copy link
Copy Markdown
Contributor Author

EspenHa commented Oct 12, 2025

Tests seem to be failing due to differences in how json is generated in Python's stdlib json module, and pydantic.

I might come back and fix this later if there is interest for moving this PR forward.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Oct 14, 2025

@EspenHa thanks for the PR

can you please PM me via Slack to discuss this further?

@EspenHa EspenHa force-pushed the fix_python_to_json_with_uuid branch 3 times, most recently from 7f429d4 to 585521b Compare October 16, 2025 14:59
@EspenHa EspenHa marked this pull request as ready for review October 16, 2025 15:09
@EspenHa EspenHa force-pushed the fix_python_to_json_with_uuid branch from 585521b to d694e9a Compare October 24, 2025 09:49
@EspenHa
Copy link
Copy Markdown
Contributor Author

EspenHa commented Oct 24, 2025

@wing328 I added tests for serializing UUIDs. This PR should be ready to merge.

@iFurySt
Copy link
Copy Markdown

iFurySt commented Nov 6, 2025

We're facing an issue with datetime fields causing JSON serialization failures. This PR should fix both UUID and datetime serialization. Do we plan to merge this PR soon? @wing328 :)

@wing328
Copy link
Copy Markdown
Member

wing328 commented Mar 12, 2026

merged via #23210 with update samples and all tests passed

@wing328 wing328 closed this Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][PYTHON] JSON serialization fails for UUID fields in generated Python

3 participants