Skip to content

fix(records): honor ignoreNull for record mappers - #361

Open
KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/record-ignore-null
Open

KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/record-ignore-null

Conversation

@KuljeetSinghXDA

Copy link
Copy Markdown

Problem

ignoreNull has no effect on record mappers.

@MappableRecord(ignoreNull: true)
typedef PartialPoint = ({double x, double? y});

var point = PartialPointMapper.fromMap({'x': 1.5});
print(point.toMap()); // {'x': 1.5, 'y': null}

MappableRecord inherits ignoreNull from MappableInterface, and RecordMapperBase.encode already passes ignoreNull to InterfaceMapperBase.encodeFields. Two pieces are missing:

  • RecordMapperElement never reads ignoreNull from the annotation or the global builder option.
  • RecordMapperGenerator never emits the ignoreNull override, unlike ClassMapperGenerator.

So the getter always falls back to the InterfaceMapperBase default of false.

Fix

  • Read ignoreNull in RecordMapperElement from the annotation, falling back to the builder-wide option (same precedence as classes).
  • Emit final bool ignoreNull = true; in the generated record mapper when enabled.

This applies to both @MappableRecord typedefs and inline record types.

Tests

  • packages/dart_mappable/test/records/record_ignore_null_test.dart: decodes a record with a missing nullable field and asserts the re-encoded map has no y key. Fails before the fix.
  • packages/dart_mappable_builder/test/record_ignore_null_test.dart: asserts the generated mapper contains the ignoreNull override for the annotation and for the global ignoreNull builder option.

Verification

  • dart test in packages/dart_mappable: 157 tests passed.
  • dart test in packages/dart_mappable_builder: 10 tests passed.
  • dart analyze in both packages: no issues.
  • dart format --output=none --set-exit-if-changed on touched files: clean.
  • Re-ran code generation in packages/dart_mappable; the generated mappers and the initializer file are up to date.

MappableRecord inherits ignoreNull from MappableInterface, and
RecordMapperBase.encode already reads the ignoreNull getter. But the
record element never read the annotation and the generator never emitted
the override, so ignoreNull always stayed false for records.

    @MappableRecord(ignoreNull: true)
    typedef PartialPoint = ({double x, double? y});

    PartialPointMapper.fromMap({'x': 1.5}).toMap();
    // {'x': 1.5, 'y': null}

Read ignoreNull from the annotation (falling back to the global builder
option) and emit the same override the class generator emits, so null
fields are omitted for typedef and inline records.
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 this pull request may close these issues.

1 participant