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)