Skip to content

Commit

Permalink
Merge pull request python-restx#192 from ProgHaj/fix-multiple-docs-fo…
Browse files Browse the repository at this point in the history
…r-one-endpoint

Add default swagger filename to Api. Contrib: ProgHaj  review: j5awry
  • Loading branch information
j5awry committed Sep 8, 2021
2 parents bc2a9a0 + 037fe95 commit 3b93888
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Api(object):
:param url_scheme: If set to a string (e.g. http, https), then the specs_url and base_url will explicitly use this
scheme regardless of how the application is deployed. This is necessary for some deployments behind a reverse
proxy.
:param str default_swagger_filename: The default swagger filename.
"""

def __init__(
Expand Down Expand Up @@ -136,6 +137,7 @@ def __init__(
serve_challenge_on_401=False,
format_checker=None,
url_scheme=None,
default_swagger_filename="swagger.json",
**kwargs
):
self.version = version
Expand Down Expand Up @@ -166,6 +168,7 @@ def __init__(
self._refresolver = None
self.format_checker = format_checker
self.namespaces = []
self.default_swagger_filename = default_swagger_filename

self.ns_paths = dict()

Expand Down Expand Up @@ -308,7 +311,7 @@ def _register_specs(self, app_or_blueprint):
app_or_blueprint,
SwaggerView,
self.default_namespace,
"/swagger.json",
"/" + self.default_swagger_filename,
endpoint=endpoint,
resource_class_args=(self,),
)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3323,3 +3323,25 @@ def get(self):

path = data["paths"]["/with-parser/"]
assert "parameters" not in path

def test_nondefault_swagger_filename(self, app, client):
api = restx.Api(doc="/doc/test", default_swagger_filename="test.json")
ns = restx.Namespace("ns1")

@ns.route("/test1")
class Ns(restx.Resource):
@ns.doc("Docs")
def get(self):
pass

api.add_namespace(ns)
api.init_app(app)

resp = client.get("/test.json")
assert resp.status_code == 200
assert resp.content_type == "application/json"
resp = client.get("/doc/test")
assert resp.status_code == 200
assert resp.content_type == "text/html; charset=utf-8"
resp = client.get("/ns1/test1")
assert resp.status_code == 200

0 comments on commit 3b93888

Please sign in to comment.