Skip to content

StaticTypeMap breaks on union #95

@geek-merlin

Description

@geek-merlin

Thanks a lot, this library is so much fun! When playing with it, i found this though:

This one breaks, in the sense that it does NOT add a type key and deserializes to wrong variant:

final class FooBarWrapper {

  public function __construct(
    #[StaticTypeMap(key: 'type', map: [
      'foo' => Foo::class,
      'bar' => Bar::class,
    ])]
    public readonly Foo|Bar $variant,
  ) {}

}

I had to work around it like this:

final class FooBarWrapper {

  public function __construct(
    #[StaticTypeMap(key: 'type', map: [
      'foo' => Foo::class,
      'bar' => Bar::class,
    ])]
    // Serde demands interface typehint, union breaks.
    public readonly CommonFooBarInterface $variant,
  ) {}

}

Not a big issue for me, but principle of least surprise violated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions