Skip to content

Commit 475fcf7

Browse files
author
Péter Volf
committed
added a RESTX_INCLUDE_ALL_MODELS config option, refs #90
1 parent 87267c5 commit 475fcf7

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

flask_restx/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def _init_app(self, app):
248248
)
249249
app.config.setdefault("RESTX_MASK_HEADER", "X-Fields")
250250
app.config.setdefault("RESTX_MASK_SWAGGER", True)
251+
app.config.setdefault("RESTX_INCLUDE_ALL_MODELS", False)
251252

252253
def __getattr__(self, name):
253254
try:

flask_restx/swagger.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ def as_dict(self):
241241
)
242242
paths[path] = serialized
243243

244+
# register all models if required
245+
if current_app.config["RESTX_INCLUDE_ALL_MODELS"]:
246+
for m in self.api.models:
247+
self.register_model(m)
248+
244249
# merge in the top-level authorizations
245250
for ns in self.api.namespaces:
246251
if ns.authorizations:

tests/test_swagger.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,3 +3256,49 @@ def test_build_request_body_parameters_schema(self):
32563256
assert result["schema"]["type"] == "object"
32573257
assert result["schema"]["properties"]["test1"]["type"] == "integer"
32583258
assert result["schema"]["properties"]["test2"]["type"] == "string"
3259+
3260+
def test_expect_unused_model(self, app, api, client):
3261+
from flask_restx import fields
3262+
3263+
api.model(
3264+
"SomeModel", {"param": fields.String, "count": fields.Integer,},
3265+
)
3266+
3267+
@api.route("/with-parser/", endpoint="with-parser")
3268+
class WithParserResource(restx.Resource):
3269+
def get(self):
3270+
return {}
3271+
3272+
app.config["RESTX_INCLUDE_ALL_MODELS"] = True
3273+
data = client.get_specs()
3274+
assert "/with-parser/" in data["paths"]
3275+
3276+
path = data["paths"]["/with-parser/"]
3277+
assert "parameters" not in path
3278+
3279+
model = data["definitions"]["SomeModel"]
3280+
assert model == {
3281+
"properties": {"count": {"type": "integer"}, "param": {"type": "string"}},
3282+
"type": "object",
3283+
}
3284+
3285+
def test_not_expect_unused_model(self, app, api, client):
3286+
# This is the default configuration, RESTX_INCLUDE_ALL_MODELS=False
3287+
3288+
from flask_restx import fields
3289+
3290+
api.model(
3291+
"SomeModel", {"param": fields.String, "count": fields.Integer,},
3292+
)
3293+
3294+
@api.route("/with-parser/", endpoint="with-parser")
3295+
class WithParserResource(restx.Resource):
3296+
def get(self):
3297+
return {}
3298+
3299+
data = client.get_specs()
3300+
assert "/with-parser/" in data["paths"]
3301+
assert "definitions" not in data
3302+
3303+
path = data["paths"]["/with-parser/"]
3304+
assert "parameters" not in path

0 commit comments

Comments
 (0)