From 415b90caaf95da0f420f39db6071e04d22887ddf Mon Sep 17 00:00:00 2001 From: Pete West Date: Tue, 14 Jul 2020 16:19:45 +0100 Subject: [PATCH] Add support for recursive models and unit test --- flask_restx/swagger.py | 2 ++ tests/test_swagger.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/flask_restx/swagger.py b/flask_restx/swagger.py index 616e5a67..8d0f065c 100644 --- a/flask_restx/swagger.py +++ b/flask_restx/swagger.py @@ -657,6 +657,8 @@ def register_model(self, model): if name not in self.api.models: raise ValueError("Model {0} not registered".format(name)) specs = self.api.models[name] + if name in self._registered_models: + return ref(model) self._registered_models[name] = specs if isinstance(specs, ModelBase): for parent in specs.__parents__: diff --git a/tests/test_swagger.py b/tests/test_swagger.py index be3c934f..f52748a9 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -2057,6 +2057,27 @@ def get(self): client.get_specs(status=500) + def test_recursive_model(self, api, client): + fields = api.model('Person', { + 'name': restx.fields.String, + 'age': restx.fields.Integer, + 'birthdate': restx.fields.DateTime, + }) + + fields["children"] = restx.fields.List( + restx.fields.Nested(fields), + default=[], + ) + + @api.route('/recursive-model/') + @api.doc(get={'model': fields}) + class ModelAsDict(restx.Resource): + @api.marshal_with(fields) + def get(self): + return {} + + client.get_specs(status=200) + def test_specs_no_duplicate_response_keys(self, api, client): """ This tests that the swagger.json document will not be written with duplicate object keys