Skip to content

Add default swagger filename to Api. #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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