flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

Nested models problem

Open wackazong opened this issue 6 years ago • 5 comments

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.

wackazong avatar Jul 06 '18 12:07 wackazong

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

wackazong avatar Jul 06 '18 12:07 wackazong

Simply create availability as a model same way you create a model for calendar ?

JaDogg avatar Jul 25 '18 08:07 JaDogg

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.

wackazong avatar Jul 25 '18 09:07 wackazong

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.

reschkek-bpv avatar Jul 09 '19 18:07 reschkek-bpv

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?

sahiltspaceo avatar Apr 02 '20 11:04 sahiltspaceo