Skip to content

Make it possible to include "unused" models in the generated swagger documentation, refs #90 #104

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 1 commit into from
Apr 12, 2020
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
1 change: 1 addition & 0 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def _init_app(self, app):
)
app.config.setdefault("RESTX_MASK_HEADER", "X-Fields")
app.config.setdefault("RESTX_MASK_SWAGGER", True)
app.config.setdefault("RESTX_INCLUDE_ALL_MODELS", False)

def __getattr__(self, name):
try:
Expand Down
5 changes: 5 additions & 0 deletions flask_restx/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ def as_dict(self):
)
paths[path] = serialized

# register all models if required
if current_app.config["RESTX_INCLUDE_ALL_MODELS"]:
for m in self.api.models:
self.register_model(m)

# merge in the top-level authorizations
for ns in self.api.namespaces:
if ns.authorizations:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3256,3 +3256,49 @@ def test_build_request_body_parameters_schema(self):
assert result["schema"]["type"] == "object"
assert result["schema"]["properties"]["test1"]["type"] == "integer"
assert result["schema"]["properties"]["test2"]["type"] == "string"

def test_expect_unused_model(self, app, api, client):
from flask_restx import fields

api.model(
"SomeModel", {"param": fields.String, "count": fields.Integer,},
)

@api.route("/with-parser/", endpoint="with-parser")
class WithParserResource(restx.Resource):
def get(self):
return {}

app.config["RESTX_INCLUDE_ALL_MODELS"] = True
data = client.get_specs()
assert "/with-parser/" in data["paths"]

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

model = data["definitions"]["SomeModel"]
assert model == {
"properties": {"count": {"type": "integer"}, "param": {"type": "string"}},
"type": "object",
}

def test_not_expect_unused_model(self, app, api, client):
# This is the default configuration, RESTX_INCLUDE_ALL_MODELS=False

from flask_restx import fields

api.model(
"SomeModel", {"param": fields.String, "count": fields.Integer,},
)

@api.route("/with-parser/", endpoint="with-parser")
class WithParserResource(restx.Resource):
def get(self):
return {}

data = client.get_specs()
assert "/with-parser/" in data["paths"]
assert "definitions" not in data

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