fix(python-flask, python-aiohttp): handle JSON content header/cookie params in generated tests and controllers
Header and cookie parameters defined with content: application/json (instead of schema) don't work end to end in python-flask / python-aiohttp. There are two problems:
- The generated test sends
None. The generic example lookup (DefaultCodegen.setParameterExampleValue) only looks atparameter.example,parameter.examplesandparameter.schema.example. Acontentparameter has noschema, and its example lives in the media type. The generated test therefore sends'X-Json-Header': None. The controller'sjson.loads(...)fails, and the test gets a 500. - The controller calls
object.from_dict(...). When the media type schema is an inline object rather than a$refmodel, the generated controller emitsx_json_header = object.from_dict(x_json_header). That also gives a 500.
I didn't find an existing issue for this.
Changes
-
Example value.
AbstractPythonConnexionServerCodegenoverridessetParameterExampleValuefor JSON content header/cookie parameters. It takes the example from the first media type, in this order:- the media type's
example; - the first entry of
examples($refresolved); - the schema's
example($refresolved); - otherwise a minimal JSON value derived from the schema type.
The value is serialized to JSON and emitted as a Python string literal through the
x-python-connexion-test-header-valuevendor extension. The flask, aiohttp and blueplanet test templates use it when present. - the media type's
-
from_dictcall. The controller callsX.from_dict(...)for a header/cookie parameter only when its type is an actual generated model, i.e.isModeland not plainobject. Inline objects, free-form values, maps and primitives get thejson.loadsresult as is.$refmodels generate exactly as before. -
Parameters defined with
schemaare unchanged.
Verification
-
New tests in
AbstractPythonConnexionServerCodegenTest:- the example taken from the media type
example,examples,$ref'd examples, schemaexample, and none; - the controller lines for inline and
$refcontent headers/cookies.
They fail without the change.
Python*Test/AbstractPython*Testpass, including forbiddenapis. - the example taken from the media type
-
The generated tests were run against Connexion 2.14.2 (flask), Connexion 3.3.0 (flask,
useConnexion3) and aiohttp, using specs with inline and$refcontent headers. They return 200, where before they returned 500. -
bin/generate-samples.sh bin/configs/python*.yaml: no diff.
Not covered here: query/path parameters with an inline object content schema may hit the same object.from_dict issue. This PR keeps to header/cookie parameters.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
Regenerated all./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exitbin/configs/python*.yamlsamples: no diff. Generator options and docs are unchanged. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
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 JSON content header/cookie parameters (declared with content: application/json) in python-flask and python-aiohttp generated tests and controllers, which previously sent None in tests and crashed controllers with 500 errors.
- Resolves example values from the media type
example, firstexamplesentry ($refresolved), schemaexample, or a minimal JSON value derived from the schema type, and emits them as JSON string literals in generated tests. - Only emits
Model.from_dict()conversion when the parameter type is an actual generated model class; inline objects, free-form objects, maps, and primitives pass the parsed JSON through unchanged. - Applies to python-flask, python-aiohttp, and python-blueplanet templates; parameters defined with
schemaare unchanged.