Skip to content

Commit 8988b63

Browse files
author
dbmoriar
committed
Switched property from specs_url_scheme to url_scheme and applied to base_url as well.
1 parent 6b87e0a commit 8988b63

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

flask_restx/api.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ class Api(object):
9494
:param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
9595
the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
9696
checkers), otherwise the default action is to not enforce any format validation.
97-
:param specs_url_scheme: If set to a string (e.g. http, https), then the specs_url will explicitly use this scheme
98-
regardless of how the application is deployed. This is necessary for some deployments such as behind an
99-
AWS elastic load balancer so that the user recieves the full swagger URL.
97+
:param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use this
98+
scheme regardless of how the application is deployed. This is necessary for some deployments behind a reverse
99+
proxy.
100100
"""
101101

102102
def __init__(
@@ -126,7 +126,7 @@ def __init__(
126126
catch_all_404s=False,
127127
serve_challenge_on_401=False,
128128
format_checker=None,
129-
specs_url_scheme=None,
129+
url_scheme=None,
130130
**kwargs
131131
):
132132
self.version = version
@@ -184,7 +184,7 @@ def __init__(
184184
api=self,
185185
path="/",
186186
)
187-
self.specs_url_scheme = specs_url_scheme
187+
self.url_scheme = url_scheme
188188
if app is not None:
189189
self.app = app
190190
self.init_app(app)
@@ -205,7 +205,9 @@ def init_app(self, app, **kwargs):
205205
:param str contact: A contact email for the API (used in Swagger documentation)
206206
:param str license: The license associated to the API (used in Swagger documentation)
207207
:param str license_url: The license page URL (used in Swagger documentation)
208-
208+
:param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use
209+
this scheme regardless of how the application is deployed. This is necessary for some deployments behind a
210+
reverse proxy.
209211
"""
210212
self.app = app
211213
self.title = kwargs.get("title", self.title)
@@ -216,6 +218,7 @@ def init_app(self, app, **kwargs):
216218
self.contact_email = kwargs.get("contact_email", self.contact_email)
217219
self.license = kwargs.get("license", self.license)
218220
self.license_url = kwargs.get("license_url", self.license_url)
221+
self.url_scheme = kwargs.get("url_scheme", self.url_scheme)
219222
self._add_specs = kwargs.get("add_specs", True)
220223

221224
# If app is a blueprint, defer the initialization
@@ -527,9 +530,9 @@ def specs_url(self):
527530
528531
:rtype: str
529532
"""
530-
external = None if self.specs_url_scheme is None else True
533+
external = None if self.url_scheme is None else True
531534
return url_for(
532-
self.endpoint("specs"), _scheme=self.specs_url_scheme, _external=external
535+
self.endpoint("specs"), _scheme=self.url_scheme, _external=external
533536
)
534537

535538
@property
@@ -539,7 +542,7 @@ def base_url(self):
539542
540543
:rtype: str
541544
"""
542-
return url_for(self.endpoint("root"), _external=True)
545+
return url_for(self.endpoint("root"), _scheme=self.url_scheme, _external=True)
543546

544547
@property
545548
def base_path(self):

tests/test_api.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def test_specs_url(self, app):
348348
specs_url = api.specs_url
349349
assert specs_url == "/swagger.json"
350350

351-
def test_specs_url_api_scheme(self, app):
352-
api = restx.Api(app, specs_url_scheme="https")
353-
specs_url = api.specs_url
354-
assert specs_url == "https://localhost/swagger.json"
351+
def test_url_scheme(self, app):
352+
api = restx.Api(app, url_scheme="https")
353+
assert api.specs_url == "https://localhost/swagger.json"
354+
assert api.base_url == "https://localhost/"
355+

0 commit comments

Comments
 (0)