[python] escape model wire names#24120
Merged
wing328 merged 2 commits intoJun 25, 2026
Merged
Conversation
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.
Contributor
Author
|
@wing328, could you review this when you have a chance? This changes model literal generation in the |
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.
a68155e to
cdaf608
Compare
Member
|
thanks for the PR cc @cbornet (2017/09) @tomplus (2018/10) @krjakbrjak (2023/02) @fa0311 (2023/10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
pythonandpython-pydantic-v1generators. Fail generation when an OpenAPI valuecannot 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
pythonandpython-pydantic-v1; generation now fails if a value can’t be encoded.__properties, discriminator property names, and discriminator mapping keys/lookups in templates.PythonStringUtilswithtoPythonStringLiteraland a single-quoted variant plus Mustache lambdas (pythonStringLiteral,pythonSingleQuotedStringLiteral); expose viaaddMustacheLambdasin both codegens and use fromtoPythonStringLiteral.vendorExtensions.x-py-wire-name-literalviaCodegenConstants.X_PY_WIRE_NAME_LITERALand use in__properties,to_dict/from_dict, and PydanticField(alias=...).Written for commit cdaf608. Summary will update on new commits.