[python] Fix unquoted string enum defaults causing NameError in python-fastapi#24268
Open
JerrySLau wants to merge 2 commits into
Open
[python] Fix unquoted string enum defaults causing NameError in python-fastapi#24268JerrySLau wants to merge 2 commits into
JerrySLau wants to merge 2 commits into
Conversation
…rameters String enums with a default were emitted as bare identifiers (e.g. Query(uploadTime, ...)), causing NameError at import. Quote defaults like plain strings and unwrap before enum member matching so model property defaults still resolve correctly. Fixes OpenAPITools#23774
Update FakeApi.md examples to show quoted enum default values in Python client docs, matching the corrected toDefaultValue() output. Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
1 issue found across 8 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java:269">
P2: Multiline string enum defaults containing an apostrophe or backslash can stop resolving to the generated enum member. The triple-quoted unwrap path returns the escaped contents unchanged, so `getEnumDefaultValue` compares a double-escaped literal against `enumVars`; consider unescaping this branch the same way as the single-quoted branch.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| return null; | ||
| } | ||
| if (value.length() >= 6 && value.startsWith("'''") && value.endsWith("'''")) { | ||
| return value.substring(3, value.length() - 3); |
Contributor
There was a problem hiding this comment.
P2: Multiline string enum defaults containing an apostrophe or backslash can stop resolving to the generated enum member. The triple-quoted unwrap path returns the escaped contents unchanged, so getEnumDefaultValue compares a double-escaped literal against enumVars; consider unescaping this branch the same way as the single-quoted branch.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java, line 269:
<comment>Multiline string enum defaults containing an apostrophe or backslash can stop resolving to the generated enum member. The triple-quoted unwrap path returns the escaped contents unchanged, so `getEnumDefaultValue` compares a double-escaped literal against `enumVars`; consider unescaping this branch the same way as the single-quoted branch.</comment>
<file context>
@@ -242,6 +243,55 @@ public String toDefaultValue(Schema p) {
+ return null;
+ }
+ if (value.length() >= 6 && value.startsWith("'''") && value.endsWith("'''")) {
+ return value.substring(3, value.length() - 3);
+ }
+ if (value.length() >= 2 && value.startsWith("'") && value.endsWith("'")) {
</file context>
Suggested change
| return value.substring(3, value.length() - 3); | |
| return value.substring(3, value.length() - 3) | |
| .replace("\\'", "'") | |
| .replace("\\\\", "\\"); |
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.
Summary
AbstractPythonCodegen.toDefaultValue()sopython-fastapiemits valid Python inQuery()/Header()/Cookie()(fixes [BUG][PYTHON-fastapi] string enum defaults are not quoted in generated fastapi code #23774).getEnumDefaultValue()so model enum member resolution still works.parameter-test-spec.yaml.Test plan
./mvnw clean package -DskipTests./bin/generate-samples.sh ./bin/configs/python*.yaml./bin/utils/export_docs_generators.shmvn -pl modules/openapi-generator -am -Dtest=PythonFastapiCodegenTest#testStringEnumQueryParameterDefaultsAreQuoted,PythonClientCodegenTest#testStringEnumDefaultIsQuoted,PythonClientCodegenTest#testStringEnumDefaultFalseIsQuoted -Dsurefire.failIfNoSpecifiedTests=false testcc Python technical committee: @cbornet @tomplus @krjakbrjak @fa0311
cc Python FastAPI: @krjakbrjak
Fixes #23774
Summary by cubic
Quote string enum default values in generated FastAPI parameters to prevent NameError from bare identifiers, while keeping enum default resolution intact. Fixes #23774.
AbstractPythonCodegen.toDefaultValue()soQuery()/Header()/Cookie()receive string literals.getEnumDefaultValue()so enum member matching still works.pythonandpython-fastapi(usesparameter-test-spec.yaml).FakeApi.md.Written for commit b4a8488. Summary will update on new commits.