Skip to content

[python] reserve generated helper and pydantic model_* property names#24358

Open
fa0311 wants to merge 2 commits into
OpenAPITools:masterfrom
fa0311:fix/python-reserved-property-names
Open

[python] reserve generated helper and pydantic model_* property names#24358
fa0311 wants to merge 2 commits into
OpenAPITools:masterfrom
fa0311:fix/python-reserved-property-names

Conversation

@fa0311

@fa0311 fa0311 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Property names that collide with the generated helpers or with pydantic's model_* namespace are not currently escaped, and they break the generated model in a couple of ways.

A property named from_dict (or to_dict, to_json, to_str, from_json) shadows the classmethod generated on every model:

Reserved.from_dict({...})   # AttributeError: from_dict

A property named model_config is worse — the class-level model_config = ConfigDict(...) assignment clobbers the field, so the value is silently accepted and then dropped from serialization. model_dump, model_fields, model_validate etc. shadow the pydantic methods the generated code itself calls.

These are all real-world-plausible field names (e.g. ML APIs with a model_config field), so this is a silent data loss / crash rather than a theoretical concern.

The fix treats those names as reserved words, reusing the sets the generator already maintains (GENERATED_MODEL_MEMBER_NAMES and the model_* entries of PYDANTIC_BASE_MODEL_MEMBER_NAMES). They get the usual var_ prefix and keep their wire name through the alias, exactly like schema / json / field today:

var_model_config: Optional[StrictStr] = Field(default=None, alias="model_config")

Names that merely share the prefix, like model_configuration, are untouched.

Added a unit test for the mangling, and updated testUnmappedMemberCollisionsRemainOutsideNameMappingValidation, which pinned the old (broken) unmapped output — it now expects the mangled fields. PythonClientCodegenTest passes and the python samples are unchanged.

PR checklist

  • Read the contribution guidelines.
  • Ran ./mvnw clean package and regenerated the python samples (no diff).
  • Filed the PR against master.

/cc @cbornet @tomplus @krjakbrjak


Summary by cubic

Reserve Python model property names that collide with generated helpers and pydantic model_* methods to prevent crashes and silent data loss. Fields like to_dict and model_config now mangle to var_* while keeping their wire names via aliases.

  • Bug Fixes
    • Reserve helper names (from_dict, to_dict, to_json, to_str, from_json) and pydantic model_* methods (model_config, model_dump, model_fields, etc.).
    • Mangle to var_<name> with alias="<wire_name>"; similar names (e.g., model_configuration) are not affected.
    • Updated tests and docs; generator tests pass; Python samples unchanged.

Written for commit c6f1d00. Summary will update on new commits.

Review in cubic

A property named from_dict (or to_dict, to_json, ...) shadows the helper
classmethods generated on every model, so Model.from_dict() raises
AttributeError. A property named model_config is even worse: the class
level 'model_config = ConfigDict(...)' assignment clobbers the field and
the value is silently dropped from serialization. model_dump / model_fields
etc. shadow the pydantic methods the generated code itself calls.

Treat those names as reserved words so they get the usual var_ prefix and
keep their wire name through the alias, the same way schema/json/field are
handled today. Names like model_configuration that merely share the prefix
are not affected.

testUnmappedMemberCollisionsRemainOutsideNameMappingValidation pinned the
old broken output for unmapped collisions; it now expects the mangled
fields (still no hard failure, which is what the test is about).

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

Re-trigger cubic

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.

1 participant