Skip to content

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

Open
khayashi4337 wants to merge 1 commit into
OpenAPITools:masterfrom
khayashi4337:fix/python-connexion-test-example-names
Open

khayashi4337 wants to merge 1 commit into
OpenAPITools:masterfrom
khayashi4337:fix/python-connexion-test-example-names

Conversation

@khayashi4337

Copy link
Copy Markdown
Contributor

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)

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

…del names in controller test examples

Model-typed non-body parameters (e.g. a $ref object in a query or header
parameter) got the example "openapi_server.Filter()", but the generated
controller tests import models by class name and never import the package,
so the test module raised NameError. Strip the package prefix in the
Connexion codegen so the example matches the import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

1 issue found across 5 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/test/java/org/openapitools/codegen/python/PythonFlaskConnexionServerCodegenTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFlaskConnexionServerCodegenTest.java:168">
P3: The new regression test never covers the `p.defaultValue != null` early return in `setParameterExampleValue`. Add a model-typed parameter with a `default` to `model-typed-params.yaml` and assert that its default-derived example is emitted unchanged.</violation>
</file>

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

View guided diff | Re-trigger cubic

}

@Test(description = "model examples in generated controller tests use the imported class name, not the package")
public void testControllerTestModelExampleUsesImportedName() throws IOException {

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.

P3: The new regression test never covers the p.defaultValue != null early return in setParameterExampleValue. Add a model-typed parameter with a default to model-typed-params.yaml and assert that its default-derived example is emitted unchanged.

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/test/java/org/openapitools/codegen/python/PythonFlaskConnexionServerCodegenTest.java, line 168:

<comment>The new regression test never covers the `p.defaultValue != null` early return in `setParameterExampleValue`. Add a model-typed parameter with a `default` to `model-typed-params.yaml` and assert that its default-derived example is emitted unchanged.</comment>

<file context>
@@ -163,4 +163,25 @@ public void testDefaultController() throws IOException {
     }
+
+    @Test(description = "model examples in generated controller tests use the imported class name, not the package")
+    public void testControllerTestModelExampleUsesImportedName() throws IOException {
+        final DefaultCodegen codegen = new PythonFlaskConnexionServerCodegen();
+        final String outputPath = generateFiles(codegen, "src/test/resources/3_0/python-flask/model-typed-params.yaml");
</file context>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant