Skip to content

[python] Fix unquoted string enum defaults causing NameError in python-fastapi#24268

Open
JerrySLau wants to merge 2 commits into
OpenAPITools:masterfrom
JerrySLau:master
Open

[python] Fix unquoted string enum defaults causing NameError in python-fastapi#24268
JerrySLau wants to merge 2 commits into
OpenAPITools:masterfrom
JerrySLau:master

Conversation

@JerrySLau

@JerrySLau JerrySLau commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Quote string enum default values in AbstractPythonCodegen.toDefaultValue() so
    python-fastapi emits valid Python in Query()/Header()/Cookie() (fixes [BUG][PYTHON-fastapi] string enum defaults are not quoted in generated fastapi code #23774).
  • Unwrap quoted defaults in getEnumDefaultValue() so model enum member resolution still works.
  • Add unit + integration tests using parameter-test-spec.yaml.
  • Regenerate Python client samples (FakeApi.md enum default examples).

Test plan

  • ./mvnw clean package -DskipTests
  • ./bin/generate-samples.sh ./bin/configs/python*.yaml
  • ./bin/utils/export_docs_generators.sh
  • mvn -pl modules/openapi-generator -am -Dtest=PythonFastapiCodegenTest#testStringEnumQueryParameterDefaultsAreQuoted,PythonClientCodegenTest#testStringEnumDefaultIsQuoted,PythonClientCodegenTest#testStringEnumDefaultFalseIsQuoted -Dsurefire.failIfNoSpecifiedTests=false test

cc 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.

  • Bug Fixes
    • Quote string enum defaults in AbstractPythonCodegen.toDefaultValue() so Query()/Header()/Cookie() receive string literals.
    • Unwrap quoted defaults in getEnumDefaultValue() so enum member matching still works.
    • Add unit and integration tests for python and python-fastapi (uses parameter-test-spec.yaml).
    • Regenerate Python client docs to reflect quoted defaults in FakeApi.md.

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

Review in cubic

JerrySLau and others added 2 commits July 10, 2026 06:19
…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>

@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.

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);

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.

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("\\\\", "\\");

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.

[BUG][PYTHON-fastapi] string enum defaults are not quoted in generated fastapi code

1 participant