fix(api): propagate version kwarg in Api.init_app (#119)#651
Open
jbbqqf wants to merge 1 commit into
Open
Conversation
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 python-restx#119.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Api.__init__accepts aversionargument (defaulting to"1.0"), butApi.init_app()silently drops anyversion=supplied in kwargs. Otherswagger-info fields (
title,description,terms_url,contact*,license*,url_scheme) are already forwarded fromkwargsontoself.*;versionwas the missing one. Match the existing pattern.Fixes #119.
Context
The bug has been around since
init_appwas introduced. Lazyinitialization is the recommended pattern when the Flask app isn't
available at construction time, so users who try
end up with
info.version: "1.0"in their generated Swagger document. Thereis no warning, which makes it confusing — they can see
title/descriptiontake effect from the same call.
Changes
flask_restx/api.py: addself.version = kwargs.get("version", self.version)in
init_app, mirroring the existingself.title = kwargs.get(...)lines.Also document
versionin theinit_appdocstring (it was alreadydocumented on
__init__).tests/test_api.py: regression testtest_init_app_overrides_versionthat both checks
api.versionand the rendered/swagger.jsoninfo.version.CHANGELOG.rst: 1.3.3 bug-fix entry.Reproduce BEFORE/AFTER yourself (copy-paste)
What I ran locally
The single failure (
tests/test_inputs.py::URLTest::test_check) ispre-existing on
masterand unrelated to this change — it depends onhttp://this-domain-should-not-exist.comactually not resolving, which isno longer true on the network where the suite ran.
Edge cases
Api(version="1.0")init_app(app, version="2.5")"2.5"Api(version="3.0")init_app(app)(no version)"3.0"(unchanged)kwargs.get(..., self.version)Api()(default"1.0")init_app(app)"1.0"(default preserved)test_root_endpoint_lazyflowRisk / blast radius
Single-line change in
init_app. The new behaviour only affects users whoalready pass
version=toinit_app— today that value is silentlydropped, so any pre-existing caller is either already not relying on
init_app to set it (no observable change), or actively wishing it worked
(now does). No public API signature changes; no existing test required
edits.
PR drafted with assistance from Claude Code (Anthropic). The change was
reviewed manually against
flask-restx's source. The reproducer blockabove is the one I used during development; reviewers can paste it
verbatim.