Skip to content

fix(pydantic): propagate model_config to generated submodels - #2427

Open
buildwithpnj wants to merge 1 commit into
tortoise:developfrom
buildwithpnj:fix/submodel-model-config-1686
Open

buildwithpnj wants to merge 1 commit into
tortoise:developfrom
buildwithpnj:fix/submodel-model-config-1686

Conversation

@buildwithpnj

Copy link
Copy Markdown

Description

Propagates the model_config parameter passed to pydantic_model_creator() down to generated nested submodels in PydanticModelCreator._get_submodel() and PydanticModelCreator._create_submodel().

Root-level title is omitted when forwarding model_config so each nested submodel retains its own schema title (instead of inheriting the parent model's title).

Motivation and Context

Fixes #1686.

Previously, PydanticModelCreator._get_submodel() forwarded exclude, include, computed, allow_cycles, and sort_alphabetically, but omitted model_config. As a result, options supplied via pydantic_model_creator(..., model_config=...) (such as json_encoders, str_to_upper, or extra) were ignored on nested relational submodels, and submodels could share cached classes with unconfigured models.

How Has This Been Tested?

Added three regression tests in tests/contrib/test_pydantic.py:

  • test_pydantic_creator_propagates_model_config_to_submodels: verifies multi-level nested (Tournament -> Event -> Team), forward FK (Event -> Tournament), and nullable FK (Event -> Reporter) submodels inherit model_config options while preserving each submodel's own title.
  • test_pydantic_creator_submodel_cache_respects_model_config: verifies submodels generated with custom model_config do not collide with default cached submodels, and a title-only model_config does not leak to submodels.
  • test_pydantic_creator_submodel_inherits_json_encoders: verifies json_encoders passed via model_config apply to nested submodel fields during model_dump_json().

Verified locally on Windows 11 / Python 3.12.14 (TORTOISE_TEST_DB=sqlite://:memory:, PYTHONDEVMODE=1):

  • python -m pytest tests/contrib/test_pydantic.py -v: 56 passed
  • python -m ruff check tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: passed
  • python -m ruff format --check tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: passed
  • python -m mypy tortoise/contrib/pydantic/creator.py tests/contrib/test_pydantic.py: 0 errors

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

This branch has not been deployed

No deployments
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.

model_config Parameter in pydantic_model_creator Not Effective in Sub Models

1 participant