Skip to content

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

Merged
wing328 merged 4 commits into
OpenAPITools:masterfrom
JerrySLau:master
Jul 14, 2026
Merged

[python] Fix unquoted string enum defaults causing NameError in python-fastapi#24268
wing328 merged 4 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 defaults in python-fastapi parameters to prevent NameError from bare identifiers. Ensure enum default resolution works for single- and triple-quoted cases. Fixes #23774.

  • Bug Fixes
    • Quote string enum defaults in AbstractPythonCodegen.toDefaultValue() so Query()/Header()/Cookie() receive string literals.
    • Unwrap quoted defaults in getEnumDefaultValue() via unwrapPythonStringLiteral, reversing apostrophe/backslash escapes for triple-quoted values so enum matching works.
    • Add formatPythonStringLiteral(), update tests (including a subclass-based test to avoid reflection), and regenerate Python client docs to show quoted defaults in FakeApi.md.

Written for commit 6a90bae. 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.

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

JerrySLau and others added 2 commits July 13, 2026 10:53
Use a test subclass to call protected helpers so forbiddenapis passes.

Co-authored-by: Cursor <cursoragent@cursor.com>
@wing328 wing328 merged commit 9d5fe1c into OpenAPITools:master Jul 14, 2026
42 checks passed
@wing328 wing328 added this to the 7.24.0 milestone Jul 14, 2026
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.

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

2 participants