You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the default get_obj_type is used (i.e. when the subclass does not overwrite the get_obj_type method), then the dumping of the schema does not raise an error when encountering an unknown type. Instead, the error gets dumped into the serialization outputs.
It looks like _dump returns an error tuple on unexpected behaviour:
If everything is fine, dump returns an ordered dict of the object, e.g. {"hello": "world"}. If handling object type fails, it returns (None, {"_schema": "Unsupported object type: something"}). How are we supposed to handle this? Why not just always raise ValidationError?
I added a workaround for this to my subclass of OneOfSchema but it's very ugly:
class MySchema(OneOfSchema):
def _dump(self, obj, *, update_fields=True, **kwargs):
res = super()._dump(obj, update_fields=update_fields, **kwargs)
if isinstance(res, tuple) and len(res) == 2 and res[0] is None and isinstance(res[1], dict):
error_dict = res[1]
field_name, message = list(error_dict.items())[0]
raise ValidationError(message=message, field_name=field_name)
return res
The Error
If the default
get_obj_type
is used (i.e. when the subclass does not overwrite theget_obj_type
method), then the dumping of the schema does not raise an error when encountering an unknown type. Instead, the error gets dumped into the serialization outputs.It looks like
_dump
returns an error tuple on unexpected behaviour:https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L99
... but
dump
expects Exceptions to be thrown:https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L77
Minimal example
The text was updated successfully, but these errors were encountered: