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.

Bug: Webargs 7 excludes unknown query args by default instead of raise exception

See original GitHub issue

Context One of my flask smorest API endpoints optionally allows a first_name query param to be provided to filter the list of returned users:

@api.arguments(RetrieveUserQueryArgsSchema, location="query")

...

class RetrieveUserQueryArgsSchema(Schema):
    """
    Defines the allowed query parameters for retrieving users.
    """

    first_name = fields.String()

If an unknown param (e.g., “last_name”) is provided, an exception is normally raised.

Problem However, after upgrading my webargs pip dependency to version 7, I noticed by default it excludes unknown query params rather than raise an exception. This is breaking my automated tests.

Solution? After ensuring I have the latest version of flask, marshmallow, smorest and webargs, neither of these solutions work:

  • explicitly setting unknown = RAISE as mentioned here
  • adding DEFAULT_UNKNOWN_BY_LOCATION = {"query": ma.RAISE} to RetrieveUserQueryArgsSchema as mentioned here

What’s the correct fix for this? Perhaps my syntax isn’t quite right or is this a flask-smorest bug given that webargs 7 was released very recently (mid December)? Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sirosencommented, Feb 13, 2021

The behaviour I was looking for was to raise an exception if there are unknown query parameters that are not in the set of combined query parameters

That’s a different problem from the OP. It can be done, but not in the way that you’ve phrased. If you want your sets of arguments merged, create a new schema with merges them – then you can set unknown=RAISE and get the restrictive behavior. You could create a new schema explicitly, with inheritance, or with a schema-factory.

@lafrech, I think this issue is safe to close?

I’m happy to help add more doc – even if it duplicates webargs docs somewhat – but it looks like you’ve already taken care of the needful for this case.

2reactions
lafrechcommented, Jan 22, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrading to Newer Releases - webargs 8.2.0 documentation
In this case, under webargs 7.0 the schema unknown setting of EXCLUDE would be ignored. Instead, unknown=RAISE would be used.
Read more >
Changelog - webargs 8.2.0 documentation
By default, webargs will pass unknown=EXCLUDE for all locations except for request bodies ( json , form , and json_or_form ) and path...
Read more >
Release 8.2.0 unknown - Webargs
Called if an error occurs while parsing args. By default, just logs and raises error. load_cookies(req: Request, schema: Schema). Load the ...
Read more >
Advanced Usage - webargs 8.2.0 documentation
EXCLUDE except when the location is json , form , json_or_form , or path . In those cases, it uses unknown=marshmallow.RAISE instead.
Read more >
Release 5.5.3 unknown - Webargs
Each parser has a default error handling method. ... DelimitedList to parse comma-separated lists in query parameters, e.g..
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