feat(python-flask): add opt-in Connexion 3 support

Merged
SH
5mo ago
+4048-38

Summary

Adds an opt-in useConnexion3 boolean option (default false) to the python-flask server generator, giving users a way to generate Connexion-3-based servers instead of the current Connexion 2 pin. Addresses #17303, which has been open since Dec 2023 with the maintainer repeatedly inviting a contribution and several community members posting partial working fixes in the thread.

Also fixes #15062 (docs never explained why this generator uses Connexion instead of vanilla Flask) — bundled in since it's a one-line addition to a PR already touching this generator's help text. #21294 (operationId double-underscore breaking Connexion's resolver) is a different root cause and is left for its own PR.

Kept as an opt-in flag rather than a default bump, matching this repo's existing convention for breaking generator-output changes (useJackson3, useSpringBoot3/4).

Security motivation (verified, not assumed)

One commenter on #17303 specifically asked for this because staying on Connexion 2 blocks upgrading past a vulnerable Werkzeug. I ran pip-audit against the actual resolved dependency tree for both paths to check this concretely:

FlaskWerkzeugVulnerabilities found
v2 (current default)2.1.12.2.310 real CVEs, including the exact one raised in the thread (CVE-2024-34069), plus CVE-2024-49766, CVE-2024-49767, CVE-2025-66221, CVE-2026-21860, CVE-2026-27199, CVE-2026-27205, PYSEC-2023-62, PYSEC-2023-221 (×2)
v3 (useConnexion3)3.1.33.1.8Zero — the only pip-audit hits in either scan are in pip/wheel themselves (base image tooling, not app dependencies)

What changes under the flag

  • requirements.mustache / setup.mustache: connexion[flask,swagger-ui,uvicorn]>=3.3.0,<4.0.0 + Flask>=2.2.0,<4.0.0 (floor pinned to 3.3.0, not 3.0.0, since that's the only version actually run/verified here, and the first with official Python 3.13/3.14 support). swagger-ui-bundle bumped to >=1.1.0 to match the floor Connexion's own extra already silently requires. The default (v2) path also gets an upper bound added to setup.py's connexion pin (<=2.14.2, matching requirements.txt) so pip install . can't silently resolve Connexion 3 onto Connexion-2-only code.
  • main.mustache: connexion.App → connexion.FlaskApp; JSON encoding moves from a Flask.json_encoder attribute (removed in v3) to a Jsonifier(cls=...) passed into add_api().
  • encoder.mustache: JSONEncoder becomes a plain json.JSONEncoder subclass instead of extending Connexion 2's removed FlaskJSONEncoder. Restored Flask's own datetime/date/Decimal/UUID handling explicitly (werkzeug.http.http_date() + str(...)) so date-time-typed model fields still serialize instead of raising TypeError — plain json.JSONEncoder has no support for these types at all.
  • CORS (featureCORS): flask_cors.CORS(app.app) silently does nothing under Connexion 3 — Connexion 3 wraps Flask in its own ASGI middleware stack, so requests can be handled before ever reaching flask-cors. Replaced with Connexion 3's documented app.add_middleware(CORSMiddleware, ...) from starlette.middleware.cors (already a Connexion dependency, no new package needed). allow_credentials is left at its default (False) to match flask-cors's own default — combining it with a wildcard origin is a CORS anti-pattern.
  • __init__test.mustache: Connexion 3's app.test_client() returns an httpx/Starlette client, not Flask's WSGI one. Added a small adapter so the existing self.client.open(...)/self.assert200(...) calls in controller_test.mustache keep working unchanged. Also drops flask_testing.TestCase (unmaintained since 2020, not Flask-3-compatible).
  • PythonFlaskConnexionServerCodegen.java: skip-marks (via the same x-skip-test mechanism the parent class already uses for other Connexion limitations) the generated test for any operation with multiple response content types, since Connexion 3 requires the handler to specify which one to return and the auto-generated stub doesn't — see CI section below.
  • One unconditional fix (applies to both v2 and v3 output): controller.mustache swaps connexion.request.is_json/.get_json() for Flask's own import flask / flask.request.*, since Connexion 3's connexion.request is now a Starlette Request and no longer exposes those methods. Module-qualified access (rather than from flask import request) avoids a parameter named request shadowing the import.

CI coverage

Added .github/workflows/samples-python-flask-connexion3-server.yaml, mirroring the existing samples-python-fastapi-server.yaml pattern, to actually install and run the generated sample's own test suite on every change to that folder. Getting this genuinely green (rather than shipping a red CI job) surfaced two real, honestly-documented limitations, both skip-marked with clear reasons in the generated tests: operations with multiple response content types (inherent Connexion 3 behavior change for stub controllers, not fixable at the template level), and a pre-existing, version-agnostic bug where the auto-generated test example for an array-typed request body is a single item instead of an array.

Verification

  • mvn -pl modules/openapi-generator -am package — compiles clean.
  • Full repo CI "Unit tests" command run verbatim: 4493 tests, 0 failures, 0 errors, 7 skipped.
  • bin/generate-samples.sh (all ~766 generators, no args) run twice — zero diff outside files intentionally touched.
  • Built and ran the generated useConnexion3: true sample's own Dockerfile: server starts, serves /v2/openapi.json, returns correct 401 on an auth-protected route, and correctly serializes a real returned model instance (including a real datetime field) end-to-end via direct HTTP request.
  • Verified CORS works correctly (and safely) after the fix: Access-Control-Allow-Origin: * present on a real response, without Access-Control-Allow-Credentials.
  • Established a genuine Connexion-2 baseline (separate Python 3.11 container — the generated Dockerfile's python:3-alpine floats to Python 3.14, which breaks even the unmodified v2 sample for unrelated reasons) to fairly compare test results between v2 and v3.
  • Replicated the new CI workflow's exact steps locally: 8 passed, 13 skipped, 0 failed.

Known gaps (not fixed here, flagged for visibility)

  • Separately discovered, unrelated to Connexion version: generating python-flask through any config with an additionalProperties block changes some */*-consumes skip-marking behavior for a couple of operations (reproduces even with useConnexion3: false). Worth its own issue/investigation; out of scope here.

Fixes #15062

Test plan

  • CI "Samples up-to-date" passes
  • CI "Unit tests" passes
  • New "Python Flask (Connexion 3) Server" workflow passes
  • Maintainer confirms useConnexion3 opt-in approach is acceptable per the contributing guidelines note on avoiding excessive generator options