feat(python-flask): add opt-in Connexion 3 support
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:
| Flask | Werkzeug | Vulnerabilities found | |
|---|---|---|---|
| v2 (current default) | 2.1.1 | 2.2.3 | 10 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.3 | 3.1.8 | Zero — 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-bundlebumped to>=1.1.0to match the floor Connexion's own extra already silently requires. The default (v2) path also gets an upper bound added tosetup.py'sconnexionpin (<=2.14.2, matchingrequirements.txt) sopip install .can't silently resolve Connexion 3 onto Connexion-2-only code. - main.mustache:
connexion.App→connexion.FlaskApp; JSON encoding moves from aFlask.json_encoderattribute (removed in v3) to aJsonifier(cls=...)passed intoadd_api(). - encoder.mustache:
JSONEncoderbecomes a plainjson.JSONEncodersubclass instead of extending Connexion 2's removedFlaskJSONEncoder. Restored Flask's own datetime/date/Decimal/UUID handling explicitly (werkzeug.http.http_date()+str(...)) sodate-time-typed model fields still serialize instead of raisingTypeError— plainjson.JSONEncoderhas 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 documentedapp.add_middleware(CORSMiddleware, ...)fromstarlette.middleware.cors(already a Connexion dependency, no new package needed).allow_credentialsis 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 existingself.client.open(...)/self.assert200(...)calls incontroller_test.mustachekeep working unchanged. Also dropsflask_testing.TestCase(unmaintained since 2020, not Flask-3-compatible). - PythonFlaskConnexionServerCodegen.java: skip-marks (via the same
x-skip-testmechanism 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.mustacheswapsconnexion.request.is_json/.get_json()for Flask's ownimport flask/flask.request.*, since Connexion 3'sconnexion.requestis now a StarletteRequestand no longer exposes those methods. Module-qualified access (rather thanfrom flask import request) avoids a parameter namedrequestshadowing 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: truesample'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 realdatetimefield) 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, withoutAccess-Control-Allow-Credentials. - Established a genuine Connexion-2 baseline (separate Python 3.11 container — the generated Dockerfile's
python:3-alpinefloats 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-flaskthrough any config with anadditionalPropertiesblock changes some*/*-consumes skip-marking behavior for a couple of operations (reproduces even withuseConnexion3: 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
useConnexion3opt-in approach is acceptable per the contributing guidelines note on avoiding excessive generator options