flask-restplus
flask-restplus copied to clipboard
Nested models problem
I have a data structure with has nesting. I cannot get restplus to create a correct Swagger documentation. I tried
ns = api.namespace('calendars', description='Operations related to calendars')
availability = ns.model('Availability', {
'monday': fields.String(required=True, description='Availability of the therapist on Monday'),
})
calendar = ns.model('Calendar', {
'id': fields.Integer(readOnly=True, description='The unique identifier'),
'availability': fields.Nested(availability, description='The availability of the therapist during the week')
})
and
ns = api.namespace('calendars', description='Operations related to calendars')
availability = {
'monday': fields.String(required=True, description='Availability of the therapist on Monday'),
}
calendar = ns.model('Calendar', {
'id': fields.Integer(readOnly=True, description='The unique identifier'),
'availability': availability)
and
ns = api.namespace('calendars', description='Operations related to calendars')
availability = {
'monday': fields.String(required=True, description='Availability of the therapist on Monday'),
}
calendar = ns.model('Calendar', {
'id': fields.Integer(readOnly=True, description='The unique identifier'),
'availability': fields.Nested(availability, description='The availability of the therapist during the week')
})
In the first case the Availability model is not in the definitions of the swagger file. In the latter two, restplus seems to expect a model and not a dict. How do I define this correctly? Seems like a trivial data model to me.
I worked around by defining a resource for the model and using it as model in the expect decorator. Then it appears in the Swagger model list and there is resolver error anymore.
@ns.route('/<int:id>/availability')
class Availability(Resource):
@ns.expect(availability)
def get(self):
pass
Simply create availability
as a model same way you create a model for calendar ?
I did create a model. I had to include it as a resource to get it added to the Swagger docs, this is what puzzled me.
This issue is likely related to https://github.com/noirbizarre/flask-restplus/issues/643 and https://github.com/noirbizarre/flask-restplus/pull/616. In particular, if the API has ordered=True
, nested models don't get written to the docs.
I did create a model. I had to include it as a resource to get it added to the Swagger docs, this is what puzzled me.
How to include it as a resource?