Skip to content

fix: pass [MappingTargetOriginalValue] to mappings referenced by Use - #2500

Open
trejjam wants to merge 1 commit into
riok:mainfrom
trejjam:fix/mapping-target-original-value-use
Open

trejjam wants to merge 1 commit into
riok:mainfrom
trejjam:fix/mapping-target-original-value-use

Conversation

@trejjam

@trejjam trejjam commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Description

A user-implemented mapping referenced with Use received default for its [MappingTargetOriginalValue] parameter, even when the target member already had a value. The same method picked by type received the member value. UseNamedMappingBuilder wraps the referenced mapping, and the member mapping builder looked for the parameter on the wrapper, not on the method inside it.

[MapProperty(nameof(A.Value), nameof(B.Count), Use = nameof(ResolveCount))]
partial void Map(A src, B target);

[UserMapping(Default = false)]
private static int? ResolveCount(int? value, [MappingTargetOriginalValue] int? original) => value ?? original;

Before:

target.Count = ResolveCount(src.Value, default(int?));

After:

target.Count = ResolveCount(src.Value, target.Count);

What changed:

  • CompositeMapping now holds the referenced mapping and its optional input and output conversions, so the member mapping builder can find the referenced mapping. Only the referenced mapping receives the target value. The conversions do not, as before.
  • The target value is passed only when the target member is readable and its type converts implicitly to the parameter type. Otherwise the parameter receives default, and Mapperly reports a new warning: RMG102 for a member that is not readable, RMG101 for a type that does not fit.
  • These two checks also apply to mappings picked by type. Before, a set-only target member threw an InvalidOperationException in the generator, and a type that does not fit produced code that does not compile.
  • Init-only members, constructor parameters and queryable projections are unchanged. They do not receive the target value and do not report the new warnings.

There is no issue for this bug yet.

Checklist

  • I did not use AI tools to generate this PR, or I have manually verified that the code is correct, optimal, and follows the project guidelines and architecture
  • I understand that low-quality, AI-generated PRs will be closed immediately without further explanation
  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

A user-implemented mapping referenced with Use received the default
value for its [MappingTargetOriginalValue] parameter. The member mapping
builder looked for the parameter on the wrapper that
UseNamedMappingBuilder creates, not on the referenced mapping.

CompositeMapping now holds the referenced mapping and its optional input
and output conversions. Only the referenced mapping receives the target
value.

The target value is passed only when the target member is readable and
its type converts implicitly to the parameter type. Otherwise the
parameter receives its default value, and Mapperly reports RMG102 for a
member that is not readable and RMG101 for a type that does not fit.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
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