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

Union of scalar and array of classes does not work #395

Closed
JanMikes opened this issue Aug 2, 2023 · 1 comment · Fixed by #493
Closed

Union of scalar and array of classes does not work #395

JanMikes opened this issue Aug 2, 2023 · 1 comment · Fixed by #493

Comments

@JanMikes
Copy link

JanMikes commented Aug 2, 2023

Hi, when trying to map union of scalar and array of classes, i get error:

CuyZ\Valinor\Mapper\TypeTreeMapperError: Could not map type `Foo`. An error occurred at path items: Invalid value array{0: array{…}}.
(new MapperBuilder())->mapper()->map(Foo::class, Source::array([
    'items' => [
        [
            'id' => 1,
            'value' => 'baz',
        ],
    ],
]));
readonly final class Foo
{
    public function __construct(
        /** @var string|list<Bar> */
        public string|array $items,
    ) {
    }
}
readonly final class Bar
{
    public function __construct(
        public int $id,
        public string $value,
    ) {
    }
}

When trying as source the string value (first type of the union) it works as expected.

(new MapperBuilder())->mapper()->map(Foo::class, Source::array([
    'items' => 'some value',
]));

Currently my workaround is to not map it to array of objects but to union of string and array shape and do the mapping later separately which works but it is not as comfortable as it could be.

@janopae
Copy link

janopae commented Dec 22, 2023

Similar issue with something like ''|DateTimeImmutable (for an API that returns date strings or empty strings): Will not map the dates appropriately (probably because they are not '').

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

Successfully merging a pull request may close this issue.

2 participants