Repository navigation
fix(records): honor ignoreNull for record mappers - #361
Open
KuljeetSinghXDA wants to merge 1 commit into
Open
KuljeetSinghXDA wants to merge 1 commit into
KuljeetSinghXDA wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ignoreNullhas no effect on record mappers.MappableRecordinheritsignoreNullfromMappableInterface, andRecordMapperBase.encodealready passesignoreNulltoInterfaceMapperBase.encodeFields. Two pieces are missing:RecordMapperElementnever readsignoreNullfrom the annotation or the global builder option.RecordMapperGeneratornever emits theignoreNulloverride, unlikeClassMapperGenerator.So the getter always falls back to the
InterfaceMapperBasedefault offalse.Fix
ignoreNullinRecordMapperElementfrom the annotation, falling back to the builder-wide option (same precedence as classes).final bool ignoreNull = true;in the generated record mapper when enabled.This applies to both
@MappableRecordtypedefs 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 noykey. Fails before the fix.packages/dart_mappable_builder/test/record_ignore_null_test.dart: asserts the generated mapper contains theignoreNulloverride for the annotation and for the globalignoreNullbuilder option.Verification
dart testinpackages/dart_mappable: 157 tests passed.dart testinpackages/dart_mappable_builder: 10 tests passed.dart analyzein both packages: no issues.dart format --output=none --set-exit-if-changedon touched files: clean.packages/dart_mappable; the generated mappers and the initializer file are up to date.