Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom coerce and Union #415

Closed
sylflo opened this issue May 18, 2022 · 1 comment
Closed

Custom coerce and Union #415

sylflo opened this issue May 18, 2022 · 1 comment

Comments

@sylflo
Copy link

sylflo commented May 18, 2022

I have the following code

import json

from dataclasses import dataclass
from typing import Any, Mapping, Sequence, Union

from apischema import deserialize


def _coerce_json(cls, data):
    if not isinstance(data, cls) and isinstance(data, str):
        return json.loads(data)
    else:
        return data


@dataclass
class MyClass:
    my_property: Union[Sequence[Any], Mapping[str, Any]]


ret = deserialize(MyClass, {"my_property": '{"test": 2}'}, coerce=_coerce_json)

When using Union[Sequence[Any], Mapping[str, Any]] and trying to execute the code, I get the following error

apischema.validation.errors.ValidationError: ValidationError(messages=[], children={'my_property': ValidationError(messages=['expected type array, found string', 'expected type object, found string'], children={})})

However if I remove the Union like this my_property: Mapping[str, Any], this is working and the object is deserialized properly

EDIT: This behavior seems to be introduced by this commit 37bb1d4

@sylflo
Copy link
Author

sylflo commented Nov 17, 2022

This PR #501 seems to fix the problem

@wyfo wyfo closed this as completed Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants