Repository navigation
fix(python-aiohttp, python-blueplanet, python-flask): read header/cookie params from the request; stop overwriting non-body params from the body - #25362
Conversation
…ams from request and avoid overwriting non-body model params Connexion only binds path, query, body, files, and context arguments to controller handlers and never passes header parameters, causing required header parameters in generated controller signatures to fail with TypeError at runtime. In addition, python-blueplanet and python-flask unconditionally deserialized non-container model parameters from request.get_json(), overwriting non-body model parameters such as $ref query parameters with the request body. Exclude header parameters from controller signatures via x-non-header-params and read them from request headers inside the handler, and scope request.get_json() deserialization to body parameters while deserializing non-body model parameters from their dict value. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…arams, header coercion/defaults, and non-body container params
Address Codex review findings on Connexion parameter handling:
- Scope request.get_json() deserialization for array and map parameters
in python-flask and python-blueplanet to body parameters, deserializing
non-body list/dict arguments from their passed value.
- Convert integer, number,boolean, and JSON-content header/cookie
parameters read from request headers/cookies and honor schema default
values when omitted.
- Use baseName instead of paramName for header keys in generated
controller_test.mustache across python-aiohttp, python-blueplanet, and
python-flask so required headers pass Connexion validation.
- Exclude cookie parameters from controller signatures and read them
from request.cookies.get('{{baseName}}') inside the handler.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…faults as Python literals and support array/JSON cookies - Format array schema defaults as valid Python list literals (quoting strings and capitalizing True/False for both Jackson ArrayNode and List defaults) so header/cookie array defaults do not emit bare identifiers or lowercase booleans. - Split non-JSON array header and cookie values on ',' and coerce integer/number/boolean array items, and decode JSON-content cookies via json.loads before model deserialization. - Guard non-body date, datetime, and model array comprehensions in python-aiohttp with isinstance(..., list). - Rename the x-non-header-params vendor extension to x-handler-params to reflect that both header and cookie parameters are excluded. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
There was a problem hiding this comment.
1 issue found across 13 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/python-aiohttp/controller.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/python-aiohttp/controller.mustache:64">
P2: Header/cookie lookups leave non-JSON complex array parameters as raw strings, so valid simple-style arrays of model items never become `List[Model]`. Parse the parameter’s wire representation before the model-item conversion, or retain Connexion’s converted value.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| """ | ||
| {{#allParams}} | ||
| {{#isHeaderParam}} | ||
| {{paramName}} = request.headers.get('{{baseName}}'{{#defaultValue}}, {{{defaultValue}}}{{/defaultValue}}) |
There was a problem hiding this comment.
P2: Header/cookie lookups leave non-JSON complex array parameters as raw strings, so valid simple-style arrays of model items never become List[Model]. Parse the parameter’s wire representation before the model-item conversion, or retain Connexion’s converted value.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/python-aiohttp/controller.mustache, line 64:
<comment>Header/cookie lookups leave non-JSON complex array parameters as raw strings, so valid simple-style arrays of model items never become `List[Model]`. Parse the parameter’s wire representation before the model-item conversion, or retain Connexion’s converted value.</comment>
<file context>
@@ -57,6 +60,44 @@ async def {{operationId}}(request: web.Request, {{#allParams}}{{paramName}}{{^re
"""
{{#allParams}}
+ {{#isHeaderParam}}
+ {{paramName}} = request.headers.get('{{baseName}}'{{#defaultValue}}, {{{defaultValue}}}{{/defaultValue}})
+ {{/isHeaderParam}}
+ {{#isCookieParam}}
</file context>
There was a problem hiding this comment.
Partially addressed in cf71763. Arrays whose items reference an enum or a primitive alias (which also have a complexType but aren't models) are now split on , like other primitive arrays before conversion.
For arrays of object models I've intentionally left the raw string alone. OpenAPI's parameter serialization (style: simple for headers, style: form for cookies) defines primitives, arrays of primitives and a single flat object, but no wire format for an array of objects; those need content: application/json, which is already decoded with json.loads before from_dict. Splitting such a value on , would produce List[str], and Model.from_dict(str) returns an empty model, which silently discards the data. Happy to change this if you'd prefer a different behavior.
… params, format nested array defaults, and split enum-ref array headers Address cubic-dev-ai review findings on PR OpenAPITools#25362: - Guard non-container date and date-time deserialization with `if <param> is not None:` across python-aiohttp, python-blueplanet, and python-flask so omitted optional date/date-time headers or cookies do not pass None into dateutil.parser.parse(). - Recursively format object and nested array elements in array schema defaults as Python dict and list literals instead of calling JsonNode.asText() (which returns '' for container nodes). - Split non-JSON array header/cookie values when items are non-object referenced schemas (such as $ref enum models where !items.isModel) while keeping object-model arrays unsplit unless JSON content is used. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
In the Connexion-based Python server generators (python-aiohttp, python-blueplanet, python-flask), generated handlers declare
in: headerandin: cookieparameters as function arguments. Connexion (1.x, 2.x and 3.x) never injects those: only path, query, body and files arguments are passed (see the request handling docs, 2.14.2connexion/operations/abstract.py#get_argumentsand 3.xconnexion/decorators/parameter.py). As a result:TypeError: <handler>() missing 1 required positional argument→ HTTP 500. Reproduced with real servers on Connexion 3.3.0 and 2.14.2 (python-flask), 2.14.2 (python-aiohttp) and 1.1.15 (python-blueplanet).None, even when the client sends it (for example,api_keyin the petstoredelete_pet).Separately, python-flask and python-blueplanet deserialize non-body model and container parameters from
request.get_json(), the request body. A$refquery parameter sent alongside an unrelated JSON body is overwritten by that body.Changes
x-handler-paramslists the remaining ones). They are read inside the handler fromrequest.headers/request.cookies(flask.request,connexion.requestor aiohttp'srequest) by their original name.baseNameis restored fromx-python-connexion-openapi-name.,and their items coerced.content: application/jsonheaders and cookies are decoded withjson.loadsbeforeModel.from_dict.get_json()-based conversion is limited to body parameters. Non-body model and container parameters are converted from the value Connexion passed.['foo', 'bar'],[True]). This override oftoDefaultValuealso affects model__init__defaults: for example, the 7.26.0 outputflags=[true]raisesNameErrorwhen the model is imported, and now becomes[True].baseName(X-Req-Header), not the Python parameter name.Migration note
For operations with header or cookie parameters, the generated handler signature changes. Those parameters are no longer function arguments and are read from the request inside the handler. Code that calls generated handlers directly and passes header or cookie arguments needs to provide them through the request instead. Before this change, Connexion never passed these arguments anyway: required ones failed and optional ones were always
None.Tests and verification
src/test/resources/3_0/python/connexion-param-sources.yaml. New tests inAbstractPythonConnexionServerCodegenTestcover all three generators plususeConnexion3. Each fails without its fix.Python*Test,AbstractPython*Test: 164 tests, 0 failures. Samples change only in the four petstore serverpet_controller.pyfiles, wheredelete_pet'sapi_keyheader is now read from the request.400 Missing header parameter. The generatedtest_default_controller.pypasses with pytest for a spec with a required header.Known limitations (not changed here)
type: integer/number/booleanheader values such as'42'with a 400 before the handler runs. This is Connexion behavior, not something the generator controls.openapi_server.<Model>()without importing it (for example,samples/server/petstore/python-flask/openapi_server/test/test_store_controller.py), so those tests fail withNameError. This was already the case in 7.26.0.deepObjectquery parameters as raw strings, so they aren't converted to models.Note on overlap
This touches the same controller-template blocks and
AbstractPythonConnexionServerCodegenTestas #25352 and #25361. Whichever lands first, I'll rebase the others.PR checklist
cc @cbornet @tomplus @krjakbrjak @fa0311 (Python technical committee)
Parts of this change were drafted with AI coding assistants (Devin, Claude Code) and reviewed and verified by me.
🤖 Generated with Claude Code
Summary by cubic
Fixes header and cookie parameter handling in the
python-aiohttp,python-blueplanet, andpython-flaskgenerators so generated servers receive these values instead of crashing or ignoring them.$refquery parameters) are no longer overwritten from the request body; only body parameters userequest.get_json().baseName.Migration
Written for commit cf71763. Summary will update on new commits.