netglade / auto_mappr

Code generation for mapping between different objects with ease.
https://pub.dev/packages/auto_mappr
MIT License
72 stars 9 forks source link

Field ignores whenNull and ignoreNull #189

Closed qwertylolman closed 8 months ago

qwertylolman commented 9 months ago

Describe the bug The generated mapper code ignores the whenNull and ignoreNull settings.

To Reproduce Code to reproduce issue:

@freezed
class FooDTO with _$FooDTO {
  const factory FooDTO({
    required String label1,
    required String label2,
// nullable value
    required SomeEnum? enumValue,
  }) = _ResourceUrlDTO;

  factory FooDTO.fromJson(Map<String, dynamic> json) => _$FooDTOFromJson(json);
}

// part of isar, that's why it has embedded and Enumerated annotations
@embedded
class BarDTO {
  BarDTO();

  late String label1;
  late String label2;
// non nullable value
  @Enumerated(EnumType.ordinal)
  late SomeEnum enumValue;

  factory BarDTO.mapper({
    required String label1,
    required String label2,
    required SomeEnum enumValue,
  }) =>
      BarDTO()
        ..label1 = label1
        ..label2 = label2
        ..enumValue = enumValue;
}

// configuration
  MapType<datasource_dto.FooDTO, isar_dto.BarDTO>(
    constructor: 'mapper',
    fields: [
      Field('label1'),
      Field('label2'),
      Field('enumValue', whenNull: SomeEnum.default, ignoreNull: true),
    ],
  ),

// generated auto_mappr output
    return _i6.BarDTO.mapper(
      label1: model.label1,
      label2: model.label2,
// highlighted error about assigning nullable value to non-nullable
      enumValue: model.enumValue,
    );

Expected behavior The generated code does respect whenNull and ignoreNull settings

Version info auto_mappr 2.3.0 auto_mappr_annotation 2.1.0

tenhobi commented 9 months ago

Hi @qwertylolman, that sounds as something that should work. We will take a look into that. 👍

Or, if you want and know where the issue might be, you can try fix the issue, just let us know, please.

petrnymsa commented 8 months ago

Turns out that issues is even with simple classes which contains non-primitive nested types.

petrnymsa commented 8 months ago

Fixed in #191 and will be available in next release