| |
@@ -5,9 +5,17 @@
|
| |
|
| |
from flask_restful.reqparse import Argument, RequestParser
|
| |
|
| |
+ from marshmallow.exceptions import ValidationError
|
| |
from .exceptions import ObjectNotFoundError, MalformedRequest
|
| |
from .schemas import AllowedMethodSchema
|
| |
|
| |
+ loads_kwargs = {}
|
| |
+ try:
|
| |
+ from marshmallow.utils import EXCLUDE
|
| |
+ loads_kwargs = {"unknown": EXCLUDE}
|
| |
+ except ImportError:
|
| |
+ pass
|
| |
+
|
| |
|
| |
class AllowedMethod(object):
|
| |
def __init__(self, method, doc, require_auth=True, params=None):
|
| |
@@ -44,14 +52,14 @@
|
| |
|
| |
def mm_deserialize(schema, json_string):
|
| |
try:
|
| |
- result = schema.loads(json_string)
|
| |
- except ValueError as err:
|
| |
+ result = schema.loads(json_string, **loads_kwargs)
|
| |
+ except (ValueError, ValidationError) as err:
|
| |
raise MalformedRequest(
|
| |
msg="Failed to parse request: {}".format(err),
|
| |
data={"request_string": json_string}
|
| |
)
|
| |
|
| |
- if result.errors:
|
| |
+ if getattr(result, "errors", False):
|
| |
raise MalformedRequest(
|
| |
msg="Failed to parse request: Validation Error",
|
| |
data={
|
| |
@@ -63,7 +71,12 @@
|
| |
|
| |
|
| |
def mm_serialize_one(schema, obj):
|
| |
- return schema().dump(obj)[0]
|
| |
+ serialized_schema = schema().dump(obj)
|
| |
+ try:
|
| |
+ serialized_schema = serialized_schema[0]
|
| |
+ except KeyError:
|
| |
+ pass
|
| |
+ return serialized_schema
|
| |
|
| |
|
| |
class MyArg(Argument):
|
| |
This hack is used on several places, I guess it could be done in the schema itself, we can call the method
dump_compat()
e.g. ... and we can document why we are doing this hack :-) it isn't obvious to me, tbh.