Skip to content

Commit

Permalink
Minor example improvement: support (json, status)
Browse files Browse the repository at this point in the history
- Very minor tweak to the import order (separating stdlib from
  non-stdlib)
- Allow a tuple of size 2 with an int in the second slot to
  trigger special handling

Technically you could use a schema which supports dumping tuple
values, so this really isn't any better or worse than not handling
this case. But it's just an example to show what sorts of things you
can do, so this may be useful for some users.

closes marshmallow-code#888
  • Loading branch information
sirosen committed Oct 11, 2023
1 parent 9463371 commit 557b675
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/schema_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
$ http GET :5001/users/ limit==1
"""
import functools
from flask import Flask, request
import random

from flask import Flask, request
from marshmallow import Schema, fields, post_dump
from webargs.flaskparser import parser, use_kwargs

Expand Down Expand Up @@ -74,6 +74,11 @@ def wrapped(*args, **kwargs):
# Function wrapped with use_args
func_with_args = use_args_wrapper(func)
ret = func_with_args(*args, **kwargs)

# support (json, status) tuples
if isinstance(ret, tuple) and len(ret) == 2 and isinstance(ret[1], int):
return schema.dump(ret[0], many=list_view), ret[1]

return schema.dump(ret, many=list_view)

return wrapped
Expand Down

0 comments on commit 557b675

Please sign in to comment.