lidatong/dataclasses-json

[BUG] Generated schema doesn't take into account custom decoder

Closed this issue · 0 comments

deansg commented

Description

If a custom decoder is specified on a dataclass, the schema generated by the class' schema() method doesn't properly take it into account. This creates a situation where deserializing a dictionary through from_dict will work differently than through schema().load(...).

Code snippet that reproduces the issue

def split_str(data: str):
    return data.split(',')


@dataclass_json
@dataclass
class SomeClass:
    a: list[str] = field(
        default=None,
        metadata=config(
            decoder=split_str
        )
    )


def test_schemas():
    b = SomeClass.from_dict({'a': '1,2,3'})
    c = SomeClass.schema().load({'a': '1,2,3'})

In this example, variable b is deserialized correctly, while trying to deserialize using Someclass.schema().load(...) throws the following exception:

marshmallow.exceptions.ValidationError: {'a': ['Not a valid list.']}

Describe the results you expected

Full consistency between .schema().load(...) and .from_dict(), both will take the custom decoder into account.

Python version you are using

3.10.5

Environment description

dataclasses-json==0.5.14