Skip to content

fix(custom-mappers): keep container available during nested mapper calls - #360

Open
KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/simple-mapper-container
Open

KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/simple-mapper-container

Conversation

@KuljeetSinghXDA

Copy link
Copy Markdown

Problem

SimpleMapper gives custom mappers access to the active MapperContainer through a static field:

abstract class _SimpleMapperBase<T extends Object> {
  static MapperContainer? _container;
  MapperContainer get container => _container!;

  T decoder(Object value, DecodingContext context) {
    _container = context.container;
    try {
      return _decode(value, context);
    } finally {
      _container = null;   // <-- clobbers an outer mapper
    }
  }
}

As soon as one custom mapper calls another custom mapper (directly, or through container.fromValue / container.toValue), the inner call resets the static field to null when it returns. The outer mapper then fails:

class OuterMapper extends SimpleMapper<Outer> {
  @override
  Outer decode(Object value) {
    var inner = container.fromValue<Inner>(value); // InnerMapper runs
    return Outer(inner, container.fromValue<int>(0));
    // Assertion failed: SimpleMapper.container can only be accessed inside
    // the decode() or encode() functions.
  }
}

This also affects SimpleMapper1 / SimpleMapper2, whose own decode/encode implementations call container.fromValue for their type arguments.

Fix

Save the previously active container and restore it in the finally block instead of assigning null. Nested decode/encode calls now unwind correctly, and the container is still unavailable outside of decode/encode.

Tests

New packages/dart_mappable/test/custom_mapper/nested_custom_mapper_test.dart:

  • decoding an outer type whose mapper calls an inner mapper and then uses container again
  • encoding the same shape
  • accessing container outside decode/encode still asserts

Both nested tests fail on the current code with
SimpleMapper.container can only be accessed inside the decode() or encode() functions.

Verification

  • dart test in packages/dart_mappable: 159 tests passed.
  • dart test in packages/dart_mappable_builder: 8 tests passed.
  • dart analyze in both packages: no issues.
  • dart format --output=none --set-exit-if-changed on touched files: clean.

SimpleMapper exposes the active MapperContainer through a single static
field. decoder() and encoder() assigned it and reset it to null in a
finally block, so a custom mapper that invokes another custom mapper lost
the container as soon as the nested call returned.

    Outer decode(Object value) {
      var inner = container.fromValue<Inner>(value);
      return Outer(inner, container.fromValue<int>(0)); // asserted
    }

This is hit by SimpleMapper1/SimpleMapper2 implementations, which call
the container from their own decode/encode methods.

Save the previous container and restore it instead of resetting to null,
so nested calls unwind correctly. The container stays unavailable outside
of decode/encode.
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