question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Looking for a way to use a schema to define acceptable query params

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
BenC14commented, Dec 2, 2019

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:

  1. I don’t want to write out a new dict object for every possible queryable field.
  2. I want to be able to define preload functions on both POSTs and GETs.
  3. I want create a schema that allows for the same param name in two possible location. For GETs obviously the location=[‘args’] for POST I could require the same location but preferably it would be location=[‘values’,‘args’] https://flask-restplus.readthedocs.io/en/stable/parsing.html#multiple-locations

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.

1reaction
ErickStoneUSUcommented, Jan 10, 2020

I second BenC14s request. Being able to use a Schema for uri parameters for a GET would be very welcomed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Describing Parameters - Swagger
Note: To describe API keys passed as query parameters, use securitySchemes and security instead. See API Keys. Query parameters can be primitive values,...
Read more >
Arrays in query params - Medium
Let's see how different groups of people have thought about formating that fields query parameter. And for each use-case, we'll see how to...
Read more >
Query Parameters and String Validations - FastAPI
When you define a query parameter explicitly with Query you can also declare it to receive a list of values, or said in...
Read more >
How to represent URI query parameters in JSON hyper-schema
As per my understanding, for GET requests, schema properties implicitly define the query parameters. Please correct me if this is not a right ......
Read more >
Running parameterized queries | BigQuery - Google Cloud
To specify a named parameter, use the @ character followed by an identifier, ... You can run a parameterized query in BigQuery in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found