fix(python-flask, python-aiohttp, python-blueplanet): use imported model names in controller test examples

KH
1d ago
+113-5

The generated controller tests of the Connexion-based Python servers (python-flask, python-aiohttp, python-blueplanet) raise NameError: name 'openapi_server' is not defined for operations with a model-typed non-body parameter (for example a $ref object in a query or header parameter, or a non-JSON request body):

from openapi_server.models.filter import Filter  # noqa: E501
...
        query_string = [('filter', openapi_server.Filter())]

AbstractPythonCodegen.setParameterExampleValue builds model examples as packageName + "." + type + "()", but controller_test.mustache imports models by class name ({{#imports}}) and never imports the package. The package's __init__.py is empty anyway, so the models are only reachable from openapi_server.models. I reproduced the NameError with tests generated by v7.26.0 as well.

Changes

  • AbstractPythonConnexionServerCodegen.setParameterExampleValue(CodegenParameter): rewrite only the exact forms the parent builds for model types, i.e. pkg.X(), [pkg.X()] and {'key': pkg.X()}, to X(), [X()] and {'key': X()}. Defaults and primitive-typed examples are left untouched, even when a user-supplied example happens to look like openapi_server.int().
  • PythonFlaskConnexionServerCodegenTest: new test with 3_0/python-flask/model-typed-params.yaml, covering query, header, array, map and body parameters, plus negative cases for string and integer examples that look like constructors.
  • samples/server/petstore/python-blueplanet/.../test/test_{store,user}_controller.py: updated the 5 affected lines by hand. The generator never overwrites existing api test files, so regenerating the samples does not refresh them. The flask samples' test files are stale in unrelated ways (variable names, JSON examples), so the change does not show there.

Verification

  • Python*Test / AbstractPython*Test pass. The new test fails without the change.
  • Generated python-flask, python-aiohttp and python-blueplanet output for the new spec contains Filter(), [Filter()] and {'key': Filter()}, and the import lines for every referenced model.
  • Ran the generated python-flask test with Connexion 2.14.2: the NameError is gone.
  • bin/generate-samples.sh for bin/configs/python-{flask,flask-connexion3,aiohttp,aiohttp-srclayout,blueplanet}.yaml produces no diff.

Not fixed here (pre-existing)

  • With the NameError gone, a generated test with an object-typed query/header parameter now reaches the server and gets 400. Connexion only deserializes style: deepObject object query parameters, so it rejects the placeholder value. I'm working on that separately (it builds on #25362).
  • Nested arrays of models ([List[Filter]()]) still produce invalid examples.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Regenerated the samples for the affected generators (bin/configs/python-{flask,flask-connexion3,aiohttp,aiohttp-srclayout,blueplanet}.yaml): no diff. Updated the 5 stale blueplanet test lines by hand (see above). 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 (Claude Code, with reviews by Codex and Fable) and reviewed and verified by me.

šŸ¤– Generated with Claude Code