Skip to content

Replace deprecated to_dict with model_dump for Pydantic v2 compatibility#21289

Closed
timonrieger wants to merge 1 commit intoOpenAPITools:masterfrom
timonrieger:python-pydantic-model_dump
Closed

Replace deprecated to_dict with model_dump for Pydantic v2 compatibility#21289
timonrieger wants to merge 1 commit intoOpenAPITools:masterfrom
timonrieger:python-pydantic-model_dump

Conversation

@timonrieger
Copy link
Copy Markdown
Contributor

@timonrieger timonrieger commented May 16, 2025

Description

This pull request updates code generation to replace the deprecated to_dict() method from Pydantic v2 models with the recommended model_dump() method.
Verified that generated models using Pydantic v2 successfully serialize with model_dump() and produce the same output as before by running a unittest with this code.

import unittest
import json
from pydantic import BaseModel, Field
  
class MyModel(BaseModel):
    id: int = Field(alias="identifier")
    name: str
    optional_field: str | None = None
  
    def to_dict(self):
        # Simulate old deprecated behavior (Pydantic v2 removed it)
        return self.model_dump(by_alias=True, exclude_unset=True)
  
    def old_json(self):
        return json.dumps(self.to_dict())
  
    def new_json(self):
        return json.dumps(self.model_dump(by_alias=True, exclude_unset=True))
  
class TestModelSerialization(unittest.TestCase):
    def test_json_output_equivalence(self):
        model = MyModel(identifier=1, name="Test")
        self.assertEqual(model.old_json(), model.new_json())
  
if __name__ == "__main__":
    unittest.main()

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 $
  • 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 change$
  • If your PR is targeting a particular programming language, @mention the [technical committee](https://github.com/openapitools/openapi-generator/#62---openapi-generator-t$

@timonrieger timonrieger changed the title remove deprecated usage of pydantic v2 to_dict method with `model_d… Replace deprecated to_dict with model_dump for Pydantic v2 compatibility May 16, 2025
@timonrieger timonrieger changed the title Replace deprecated to_dict with model_dump for Pydantic v2 compatibility Replace deprecated to_dict with model_dump for Pydantic v2 compatibility May 16, 2025
@timonrieger timonrieger marked this pull request as ready for review May 16, 2025 12:32
@wing328
Copy link
Copy Markdown
Member

wing328 commented May 17, 2025

thanks for the pr

can you please the ci test failure when you've time?

@timonrieger
Copy link
Copy Markdown
Contributor Author

yes will do!

@timonrieger
Copy link
Copy Markdown
Contributor Author

oh my bad actually. to_dict is not a pydantic method but a proprietary function in the codebase. In this function model_dump is already called. Thus, since this PR is is obsolete now, I will close it. The only thing to be done might be the removal of the comment line # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead

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.

2 participants