Skip to content

feat: support [MappingTargetOriginalValue] parameter in user-defined mapping methods#2306

Merged
latonz merged 1 commit into
riok:mainfrom
juliancarrivick:user-mapping-target-value
Jul 13, 2026
Merged

feat: support [MappingTargetOriginalValue] parameter in user-defined mapping methods#2306
latonz merged 1 commit into
riok:mainfrom
juliancarrivick:user-mapping-target-value

Conversation

@juliancarrivick

@juliancarrivick juliancarrivick commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Support [MappingTargetOriginalValue] parameter in user-defined mapping methods

Description

Following feedback in #2242, fixing #276 instead to cover the Optional use-case.

Here we add MappingTargetOriginalValueAttribute, which marks a parameter in a user-implemented mapping method to receive the existing target member value. Useful for merge-style mappings that need to combine source and destination data rather than fully overwrite.

Support for [MappingTarget] is not included as the use-case there is to work with entire object references instead of members. In that case both the "target" and the "original" value will end up being the same reference lead to potential confusion.

Thank you for considering this PR.

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
    • Used to implement tests and generate first pass at implementation. Iterated heavily after that point to avoid leaking state everywhere (particularly around setting MemberAssignmentMapping.TargetValueGetter in MemberMappingBuilder)
  • 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)

@latonz latonz added the enhancement New feature or request label Jun 22, 2026

@latonz latonz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this contribution! I added my feedback.

Comment thread docs/docs/configuration/additional-mapping-parameters.mdx Outdated
Comment thread docs/docs/configuration/additional-mapping-parameters.mdx Outdated
Comment thread src/Riok.Mapperly.Abstractions/MappingTargetOriginalValueAttribute.cs Outdated
Comment thread src/Riok.Mapperly/AnalyzerReleases.Unshipped.md Outdated
Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/SourceValueBuilder.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs Outdated
@latonz

latonz commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

The vulnerability scanning which fails the test GH Action job should be gone once you rebase onto main.

@juliancarrivick

Copy link
Copy Markdown
Contributor Author

Thanks for the review @latonz ! I'm currently on holiday but will address your comments after I'm back next week :)

@latonz

latonz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

enjoy your time off!

@juliancarrivick
juliancarrivick force-pushed the user-mapping-target-value branch from e5a4ba3 to 4e98d97 Compare July 4, 2026 09:48

@juliancarrivick juliancarrivick left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I think I've covered everything - let me know if you require any further changes (and build should pass this time).
Thanks!

Comment thread docs/docs/configuration/additional-mapping-parameters.mdx Outdated
Comment thread docs/docs/configuration/additional-mapping-parameters.mdx Outdated
Comment thread docs/docs/configuration/additional-mapping-parameters.mdx Outdated
Comment thread src/Riok.Mapperly.Abstractions/MappingTargetOriginalValueAttribute.cs Outdated
Comment thread src/Riok.Mapperly/Symbols/MappingMethodParameters.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/MappingBodyBuilders/SourceValueBuilder.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs Outdated
Comment thread test/Riok.Mapperly.Tests/Mapping/UserMethodMappingTargetOriginalValueTest.cs Outdated

@latonz latonz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, I added my feedback.

Comment thread src/Riok.Mapperly/Descriptors/Mappings/TypeMappingBuildContext.cs
Comment thread src/Riok.Mapperly/Descriptors/UserMappingMethodParameterExtractor.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/UserMappingMethodParameterExtractor.cs Outdated
Comment thread src/Riok.Mapperly/Descriptors/UserMappingMethodParameterExtractor.cs Outdated
…mapping methods

Adds MappingTargetOriginalValueAttribute, which marks a parameter in a
user-implemented mapping method to receive the existing target member value.
Useful for merge-style mappings that need to combine source and destination
data rather than fully overwrite.

Support for [MappingTarget] is not included as the common use-case there
is to take the entire target object and this will end up being the
exact same reference as the "original value".

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@juliancarrivick
juliancarrivick force-pushed the user-mapping-target-value branch from 4e98d97 to 40b6a78 Compare July 11, 2026 11:38
@latonz
latonz merged commit 78b4cbb into riok:main Jul 13, 2026
17 checks passed
@latonz

latonz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Thank you for this contribution!

@juliancarrivick
juliancarrivick deleted the user-mapping-target-value branch July 13, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants