marshmallow-code / marshmallow-code/marshmallow-oneofschema

_dump should raise errors instead of returning them

Open
#48 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
142
Forks
44
Avg merge
7h 7m
Merged PRs (30d)
3

Description

### The Error

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:

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

```python
# minimal_example.py

import marshmallow
import marshmallow.fields
from marshmallow_oneofschema import OneOfSchema

class Foo(object):
def __init__(self, foo):
self.foo = foo

class Bar(object):
def __init__(self, bar):
self.bar = bar

class FooSchema(marshmallow.Schema):
foo = marshmallow.fields.String(required=True)

@marshmallow.post_load
def make_foo(self, data):
return Foo(**data)

class BarSchema(marshmallow.Schema):
bar = marshmallow.fields.Integer(required=True)

@marshmallow.post_load
def make_bar(self, data):
return Bar(**data)

class MyUberSchema(OneOfSchema):
type_schemas = {
'Foo': FooSchema
}

if __name__ == '__main__':
serialized = MyUberSchema().dump([
Foo(foo='hello'),
Bar(bar=123)],
many=True)

print(serialized)
```

```bash
~$ pip freeze | grep marshmallow
marshmallow==3.0.0b19
marshmallow-oneofschema==2.0.0b2

~$ python --version
Python 3.6.7 :: Anaconda, Inc.

~$ python minimal_example.py
[{'foo': 'hello', 'type': 'Foo'}, (None, {'_schema': 'Unsupported object type: Bar'})]
```

Contributor guide

Open the contributing guide

Research direction

Start in marshmallow_oneofschema/one_of_schema.py, comparing the _dump path around line 99 with dump around line 77, and reproduce the behavior using minimal_example.py from the issue. Done means an unsupported object type raises an exception during dumping instead of appearing as an error tuple in the serialized output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.