Skip to content

Commit 415b90c

Browse files
committed
Add support for recursive models and unit test
1 parent f0c08b1 commit 415b90c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

flask_restx/swagger.py

+2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ def register_model(self, model):
657657
if name not in self.api.models:
658658
raise ValueError("Model {0} not registered".format(name))
659659
specs = self.api.models[name]
660+
if name in self._registered_models:
661+
return ref(model)
660662
self._registered_models[name] = specs
661663
if isinstance(specs, ModelBase):
662664
for parent in specs.__parents__:

tests/test_swagger.py

+21
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,27 @@ def get(self):
20572057

20582058
client.get_specs(status=500)
20592059

2060+
def test_recursive_model(self, api, client):
2061+
fields = api.model('Person', {
2062+
'name': restx.fields.String,
2063+
'age': restx.fields.Integer,
2064+
'birthdate': restx.fields.DateTime,
2065+
})
2066+
2067+
fields["children"] = restx.fields.List(
2068+
restx.fields.Nested(fields),
2069+
default=[],
2070+
)
2071+
2072+
@api.route('/recursive-model/')
2073+
@api.doc(get={'model': fields})
2074+
class ModelAsDict(restx.Resource):
2075+
@api.marshal_with(fields)
2076+
def get(self):
2077+
return {}
2078+
2079+
client.get_specs(status=200)
2080+
20602081
def test_specs_no_duplicate_response_keys(self, api, client):
20612082
"""
20622083
This tests that the swagger.json document will not be written with duplicate object keys

0 commit comments

Comments
 (0)