Looking for a way to use a schema to define acceptable query params
See original GitHub issueI have a case where I defined a marshmallow schema that is being used in both @accepts and @responds for different methods. I would like to use the schema object in an @accepts for a get_all route but instead of the fields being defined in the body of the request they should be query params. For example a UserSchema might have 3 fields.
class UserSchema(Schema):
userId = ma_fields.Integer(dump_only=True, attribute='user_id')
firstName = ma_fields.String(attribute='first_name')
emailAddress = ma_fields.Email(attribute='email')
and we might have a get_all route like this
@accepts(dict(name='limit', type=int), dict(name='page', type=int), api=api)
@responds(schema=UserSchema(many=True), api=api)
def get(self):
return UserService.get_all(**request.parsed_args)
I would like to have a route with documented parameters based on the schema Perhaps something like
@accepts(dict(name='limit', type=int), dict(name='page', type=int), UserSchema(location='values'), api=api)
@responds(schema=UserSchema(many=True), api=api)
def get(self):
return UserService.get_all(**request.parsed_args)
It seems like if we are returning a UserSchema object/s with this request we should be able to easily make all of that schemas dump-able fields queryable.
End result would document the existence of ?userId ?firstName and ?emailAddress. and request.parsed_args would be a dict with ‘user_id’, ‘first_name’, ‘email’ as well as the other query arg dicts ‘limit’ and ‘page’
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (7 by maintainers)
I agree with you that a GET request cannot accept a body. In this case I am trying to accomplish case #3 more easily. I want to use Marshmallow schemas to add query params for several reasons:
With the current implementation of accepts if you pass a schema object it assumes everything in that schema goes in the payload. Unfortunately that means you cannot define a GET with a schema in the request because the swagger example that gets generated by flask-restplus requires a body and most browsers will throw an error just for attempting to pass a body with a GET.
I am proposing we allow the passing of a schema to @accepts on GETs. By default all the fields in the schema are assumed to be location=‘args’ if you want to support multiple locations you can override the schema field location in the decorator with a dict(name=schema_field_name, location=[‘headers’, ‘args’, ‘values’…etc]) This way we can support multiple locations for POSTs and PUTs, one location for GETs, plus we gain support for @pre_load, @post_load, @pre_dump, @post_dump for all methods in the schema.
I second BenC14s request. Being able to use a Schema for uri parameters for a GET would be very welcomed.