Skip to content

Add OnlyExplicitMappedMembers Configuration Option#2261

Open
Kataane wants to merge 2 commits intoriok:mainfrom
Kataane:feature/only-explicit-mapped-members
Open

Add OnlyExplicitMappedMembers Configuration Option#2261
Kataane wants to merge 2 commits intoriok:mainfrom
Kataane:feature/only-explicit-mapped-members

Conversation

@Kataane
Copy link
Copy Markdown
Contributor

@Kataane Kataane commented May 3, 2026

Add OnlyExplicitMappedMembers Configuration Option

Description

First, I want to express my gratitude for the beautiful library and the excellent work that has been done in Mapperly. This library has been incredibly useful!

This PR introduces a new OnlyExplicitMappedMembers configuration option to the MapperAttribute that allows mapping only properties with explicit configurations (via attributes like MapProperty). All other properties will be ignored by default when this option is enabled.

This feature is particularly useful when working with classes that have many properties but you only need to map a few specific ones, providing a more declarative and maintainable approach compared to explicitly ignoring every unwanted property.

Changes made:

  • Added OnlyExplicitMappedMembers property to MapperAttribute
  • Added corresponding configuration to MapperConfiguration and merger logic
  • Implemented the ignore logic in IgnoredMembersBuilder to automatically ignore non-matching members when this option is enabled
  • Added comprehensive unit tests covering different scenarios (extra source members, extra target members, and extra members in both)
  • Updated test infrastructure to support the new configuration option

Fixes #1981

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)

Kataane added 2 commits May 3, 2026 14:25
- Fix ignore logic to exclude all members not in explicit mappings (MapProperty/MapPropertyFromSource), not just those missing from the opposite type
- Add tests with explicit [MapProperty] and [MapPropertyFromSource] attributes
- Use <see cref> tag for MapPropertyAttribute in XML doc comment
@Kataane
Copy link
Copy Markdown
Contributor Author

Kataane commented May 3, 2026

ref to old #2001

@latonz latonz self-requested a review May 5, 2026 17:26
@latonz latonz added the enhancement New feature or request label May 5, 2026
Copy link
Copy Markdown
Contributor

@latonz latonz left a comment

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.

  • please update the docs
  • add a test combining OnlyExplicitMappedMembers and RequiredMapping
  • add a test combining IgnoreObsoleteMembers and RequiredMapping


if (ctx.Configuration.Mapper.OnlyExplicitMappedMembers)
{
var explicitlyMappedMembers = ctx.Configuration.Members.GetMembersWithExplicitConfigurations(sourceTarget).ToHashSet();
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.

MapNestedPropertiesAttribute also need to be considered… Also add a test for this.
Could probably be included in GetMembersWithExplicitConfigurations.

public bool AutoUserMappings { get; set; } = true;

/// <summary>
/// When set to <c>true</c>, only properties with explicit configurations (via attributes like <see cref="MapPropertyAttribute"/>)
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.

Mapperly also maps fields, use members instead of properties.

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.

Simple way to exclude all properties of source or target class except explicitly mapped

2 participants