From 35960270de40ca7ee2baa9a2c59e22d61e406f27 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Braun Date: Fri, 22 May 2026 12:46:41 +0200 Subject: [PATCH] fix(api): propagate version kwarg in Api.init_app (#119) The Api constructor accepts a version (default '1.0'), but Api.init_app() silently dropped any version supplied in kwargs. As a result, calling api = Api() api.init_app(app, version='2.0') left api.version (and the resulting Swagger info.version) on the default. Match the existing pattern for title/description: pull version from kwargs with the current value as fallback. Add a regression test that fails on master and passes after the fix. Fixes #119. --- CHANGELOG.rst | 1 + flask_restx/api.py | 4 ++++ tests/test_api.py | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ea0152da..d5a4eaa2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -46,6 +46,7 @@ Bug Fixes :: * Adjust field tests for Python 3.14 (``staticmethod`` around ``functools.partial`` used as a class attribute). [python-restx] + * ``Api.init_app(app, version=...)`` now overrides the version supplied to ``Api()`` (#119) [jbbqqf] .. _section-1.3.1: 1.3.1 diff --git a/flask_restx/api.py b/flask_restx/api.py index 4f758714..dd72bc8b 100644 --- a/flask_restx/api.py +++ b/flask_restx/api.py @@ -208,6 +208,7 @@ def init_app(self, app, **kwargs): >>> api.init_app(app) :param flask.Flask app: the Flask application object + :param str version: The API version (used in Swagger documentation) :param str title: The API title (used in Swagger documentation) :param str description: The API description (used in Swagger documentation) :param str terms_url: The API terms page URL (used in Swagger documentation) @@ -219,6 +220,9 @@ def init_app(self, app, **kwargs): reverse proxy. """ self.app = app + # version is set in __init__ but used to be dropped here, leaving the + # Swagger info.version stuck on the constructor's default — see #119. + self.version = kwargs.get("version", self.version) self.title = kwargs.get("title", self.title) self.description = kwargs.get("description", self.description) self.terms_url = kwargs.get("terms_url", self.terms_url) diff --git a/tests/test_api.py b/tests/test_api.py index 2b40b235..7d2ce48b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -97,6 +97,18 @@ def test_specs_endpoint_not_added(self, app): assert "specs" not in api.endpoints assert "specs" not in app.view_functions + def test_init_app_overrides_version(self, app): + # init_app() should propagate the version kwarg the same way it + # already propagates title/description (see #119). Prior to the + # fix, the version supplied to init_app() was silently dropped, + # so the Swagger "info.version" kept the constructor's value. + api = restx.Api(version="1.0") + api.init_app(app, version="2.5") + assert api.version == "2.5" + with app.test_client() as client: + spec = client.get("/swagger.json").get_json() + assert spec["info"]["version"] == "2.5" + def test_specs_endpoint_not_found_if_not_added(self, app, client): api = restx.Api() api.init_app(app, add_specs=False)