dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

Enum mappers aren't initialized when part of a record typedef mapper

Open TNorbury opened this issue 11 months ago • 0 comments

Take the following set of code:

@MappableRecord()
typedef Foo = ({Direction dir, bool flag});

@MappableEnum()
enum Direction {
  left,
  right,
}

@MappableClass()
class MyClass with MyClassMappable {
  final Foo foo;
  MyClass(this.foo);
}

@MappableClass()
class MyClass2 with MyClass2Mappable {
  final ({Direction dir, bool flag}) foo;
  MyClass2(this.foo);
}

This generates the following mappers (only ensure initialized shown):

  static MyClassMapper ensureInitialized() {
    if (_instance == null) {
      MapperContainer.globals.use(_instance = MyClassMapper._());
      FooMapper.ensureInitialized();
    }
    return _instance!;
  }


  static MyClass2Mapper ensureInitialized() {
    if (_instance == null) {
      MapperContainer.globals.use(_instance = MyClass2Mapper._());
      _t$_R0Mapper.ensureInitialized();
      DirectionMapper.ensureInitialized();
    }
    return _instance!;
  }

Notice that DirectionMapper is only initialized when a typedef isn't used (for MyClass2). This doesn't seem like correct behavior to me, as if I try to encode MyClass, I get the following error: MapperException: Failed to encode (MyClass).foo(({Direction dir, bool flag})).dir[Direction.left]: Unknown type Direction. Did you forgot to annotate the class or register a custom mapper?

Not sure if there is an extra bit of configuration I need to be doing, or if this is a bug.

TNorbury avatar Jan 22 '25 20:01 TNorbury