Repository navigation
fix(custom-mappers): keep container available during nested mapper calls - #360
Open
KuljeetSinghXDA wants to merge 1 commit into
Open
KuljeetSinghXDA wants to merge 1 commit into
KuljeetSinghXDA wants to merge 1 commit into
Conversation
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.
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
SimpleMappergives custom mappers access to the activeMapperContainerthrough a static field:As soon as one custom mapper calls another custom mapper (directly, or through
container.fromValue/container.toValue), the inner call resets the static field tonullwhen it returns. The outer mapper then fails:This also affects
SimpleMapper1/SimpleMapper2, whose owndecode/encodeimplementations callcontainer.fromValuefor their type arguments.Fix
Save the previously active container and restore it in the
finallyblock instead of assigningnull. Nested decode/encode calls now unwind correctly, and the container is still unavailable outside ofdecode/encode.Tests
New
packages/dart_mappable/test/custom_mapper/nested_custom_mapper_test.dart:containeragaincontaineroutsidedecode/encodestill assertsBoth nested tests fail on the current code with
SimpleMapper.container can only be accessed inside the decode() or encode() functions.Verification
dart testinpackages/dart_mappable: 159 tests passed.dart testinpackages/dart_mappable_builder: 8 tests passed.dart analyzein both packages: no issues.dart format --output=none --set-exit-if-changedon touched files: clean.