Repository navigation
fix(python-flask, python-aiohttp): generate valid security function names for scheme names with '-' or '.' - #25458
Open
khayashi4337 wants to merge 4 commits into
Conversation
…ames for scheme names with '-' or '.'
Security scheme names may contain '-' and '.' (component keys match
^[a-zA-Z0-9.\-_]+$), but the generated security_controller.py used them
verbatim in function names, e.g. 'def info_from_api-key(', and the
x-apikeyInfoFunc / x-basicInfoFunc / x-bearerInfoFunc / x-tokenInfoFunc /
x-scopeValidateFunc extensions pointed at the same invalid names. The
module didn't compile, so the app couldn't start.
Each scheme name now gets one Python identifier suffix, computed once in
preprocessOpenAPI in spec order: characters other than [A-Za-z0-9_]
become '_', and names that would collide (e.g. 'api-key' and 'api.key')
are numbered (_2, _3, ...). Both addSecurityExtensions and the
security_controller templates use it, the latter through a fromSecurity
override that sets x-python-connexion-security-function-suffix. Names
that are already valid identifiers produce exactly the same output as
before, and user-provided x-*InfoFunc extensions are kept.
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
…ion-security-function-names # Conflicts: # modules/openapi-generator/src/test/java/org/openapitools/codegen/python/AbstractPythonConnexionServerCodegenTest.java
… already-valid security scheme names Reserve the security function name suffix of scheme names that are already valid Python identifiers before numbering the names that need character replacement. Previously suffixes were assigned purely in spec order, so a sanitized name like "api-key" -> "api_key" claimed the suffix first and "api_key" listed after it was bumped to "api_key_2" (and a literal "api_key_2" to "api_key_2_2"), changing the generated output for names that were already valid. Also make testSecuritySchemeNamesProduceValidPythonFunctions parse the generated openapi.yaml and assert, per scheme, that each x-*Func extension points at the expected function defined in security_controller.py, instead of only checking that the reference strings occur somewhere in the file. Add an "api_key" scheme listed after "api-key" to the fixture to cover the ordering regression. Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
…unction name suffixes are computed
This branch has not been deployed
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.
Security scheme names may contain
-and.: OpenAPI component keys match^[a-zA-Z0-9\.\-_]+$. The generated python-flask / python-aiohttpsecurity_controller.pyuses the name verbatim in function names, and thex-*InfoFuncextensions in the generatedopenapi.yamlpoint at the same names. For example, with a scheme namedapi-key:The module doesn't compile, so the generated app can't start. I didn't find an existing issue for this.
Changes
preprocessOpenAPI, in spec order. Characters other than[A-Za-z0-9_]become_. Names that would collide (e.g.api-keyandapi.key) are numbered_2,_3, and so on.addSecurityExtensions(x-apikeyInfoFunc,x-basicInfoFunc,x-bearerInfoFunc,x-tokenInfoFunc,x-scopeValidateFunc) and thesecurity_controller.mustachetemplates both use that suffix. The templates get it through afromSecurityoverride that setsx-python-connexion-security-function-suffix.x-*InfoFuncextensions are kept.Verification
Python*Test/AbstractPython*Testpass. The new test covers python-flask, python-flask withuseConnexion3, and python-aiohttp. It uses schemesapi-key,petstore.auth(OAuth2),basic-auth,bearer-token,1st-key, a literalapi_key_2next to the collidingapi.key, and a scheme with a user-providedx-apikeyInfoFunc. It fails without the change..pyfiles passcompileall. On Connexion 2.14.2, every protected operation (apiKey, basic, bearer, OAuth2, the colliding and the user-provided ones) returns 401 without credentials and 200 with them. With the generator from master, the same project doesn't compile. A second review also exercised Connexion 3.3.0 and aiohttp.bin/generate-samples.sh bin/configs/python*.yamlproduces no diff.Note: #25457 also overrides
fromSecurityin the same class. Whichever lands second needs the two bodies merged into one override.PR checklist
bin/configs/python*.yamlsamples: no diff. Generator options and docs are unchanged.cc @cbornet @tomplus @krjakbrjak @fa0311 (Python technical committee)
Parts of this change were drafted with AI coding assistants (Devin, Claude Code, with reviews by Codex and Fable) and reviewed and verified by me.
🤖 Generated with Claude Code
Summary by cubic
Fixes generated python-flask and python-aiohttp apps failing to compile when a security scheme name contains
-or.(e.g.api-key). Scheme names were used verbatim insecurity_controller.pyfunction names and in thex-*InfoFunc/x-scopeValidateFuncreferences in the spec, producing invalid Python likedef info_from_api-key(...).Each scheme name now maps to a valid Python identifier suffix computed once per spec: non-
[A-Za-z0-9_]characters become_, names that are already valid identifiers keep their unnumbered suffix, and sanitized names that would collide (e.g.api-keyandapi.key) get_2,_3, etc. Both thesecurity_controller.mustachetemplates and thex-*Funcextensions use the same suffix via afromSecurityoverride, and user-providedx-*InfoFuncextensions are preserved.The new test parses the generated
openapi.yamland asserts eachx-*Funcreference points at the matching generated function, covering python-flask, python-flask withuseConnexion3, and python-aiohttp; generated samples are unchanged. Note: #25457 also overridesfromSecurityin the same class — whichever lands second must merge the two method bodies.Written for commit 956dcb6. Summary will update on new commits.