Skip to content

[python] escape model wire names#24120

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
tamird:upstream/python-escaped-model-wire-names
Jun 25, 2026
Merged

[python] escape model wire names#24120
wing328 merged 2 commits into
OpenAPITools:masterfrom
tamird:upstream/python-escaped-model-wire-names

Conversation

@tamird

@tamird tamird commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Python model templates emit OpenAPI property names and discriminator
names and values as unescaped Python string literals. Quotes,
backslashes, and control characters can therefore make generated
modules invalid or change the wire keys used at runtime.

Escape field aliases, model dictionary keys, discriminator lookups, and
discriminator mappings as Python string literals in the python and
python-pydantic-v1 generators. Fail generation when an OpenAPI value
cannot be represented as a valid Python string literal instead of
emitting invalid source.

Preserve generated source for values that need no escaping.


Summary by cubic

Escape OpenAPI property names and discriminator names/values in generated Python models so they are valid Python string literals, preventing invalid code and wrong wire keys at runtime. Applies to python and python-pydantic-v1; generation now fails if a value can’t be encoded.

  • Bug Fixes
    • Escape field aliases, model dict keys, __properties, discriminator property names, and discriminator mapping keys/lookups in templates.
    • Add PythonStringUtils with toPythonStringLiteral and a single-quoted variant plus Mustache lambdas (pythonStringLiteral, pythonSingleQuotedStringLiteral); expose via addMustacheLambdas in both codegens and use from toPythonStringLiteral.
    • Store escaped wire names in vendorExtensions.x-py-wire-name-literal via CodegenConstants.X_PY_WIRE_NAME_LITERAL and use in __properties, to_dict/from_dict, and Pydantic Field(alias=...).
    • Regenerate samples and expand tests to cover quotes, backslashes, and control characters (e.g., newlines) in wire keys and discriminator values.

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

Review in cubic

Python model templates in the modern and Pydantic v1 clients
interpolate OpenAPI property names and discriminator names and values
directly into string literals. Quotes, backslashes, and control
characters can therefore produce invalid generated modules.

Render field aliases, dictionary keys, discriminator lookups, and
discriminator mappings through Python string-literal escaping in both
generators. Stop generation if a value cannot be encoded rather than
emitting unsafe source.
@tamird

tamird commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

@wing328, could you review this when you have a chance? This changes model literal generation in the python and python-pydantic-v1 generators.

@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 46 files

Re-trigger cubic

Regenerate the checked-in modern and Pydantic v1 Python petstore
clients after escaping model wire names. Existing inherited, oneOf,
anyOf, and nested models exercise quotes, backslashes, and control
characters in wire keys and discriminator values.
@tamird tamird force-pushed the upstream/python-escaped-model-wire-names branch from a68155e to cdaf608 Compare June 24, 2026 16:42
@wing328 wing328 added this to the 7.24.0 milestone Jun 24, 2026
@wing328

wing328 commented Jun 24, 2026

Copy link
Copy Markdown
Member

thanks for the PR

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

@wing328 wing328 merged commit 3a307ab into OpenAPITools:master Jun 25, 2026
46 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