flask-restful-swagger icon indicating copy to clipboard operation
flask-restful-swagger copied to clipboard

Make resource fields mandatory

Open anvth opened this issue 7 years ago • 1 comments

I have the following model:

@swagger.model
class AuthModel:
    resource_fields = {
        'username': fields.Integer,
        'password':  fields.String
    }

In the generated spec, the model will have both the resource fields as optional. How do I make them mandatory?

anvth avatar Jun 15 '18 09:06 anvth

Very old question, but I discovered the answer to this myself today by looking through the source code and thought I'd share. To remove the 'optional' annotation which is automatically added to resource fields, you need to specify a required property in your class definition, which should contain a list of field names.

For example, to make the "username" field mandatory, you'd do the following:

@swagger.model
class AuthModel:
    resource_fields = {
        'username': fields.Integer,
        'password':  fields.String
    }
    required = ['username']

Or alternatively, to make all fields in your model mandatory:

@swagger.model
class AuthModel:
    resource_fields = {
        'username': fields.Integer,
        'password':  fields.String
    }
    required = list(resource_fields.keys())

callynch avatar Aug 08 '23 05:08 callynch