diff --git a/flask_restplus/swagger.py b/flask_restplus/swagger.py index 77c364bf..c54c659b 100644 --- a/flask_restplus/swagger.py +++ b/flask_restplus/swagger.py @@ -575,6 +575,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 9e583f55..79c11749 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -2042,6 +2042,26 @@ def get(self): client.get_specs(status=500) + def test_recursive_model(self, api, client): + fields = api.model('Person', { + 'name': restplus.fields.String, + 'age': restplus.fields.Integer, + 'birthdate': restplus.fields.DateTime + }) + + fields["children"] = restplus.fields.List( + restplus.fields.Nested(fields), + default=[]) + + @api.route('/recursive-model/') + @api.doc(get={'model': fields}) + class ModelAsDict(restplus.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