Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Kataane
Copy link
Contributor

@Kataane Kataane commented Oct 23, 2025

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

  • 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 Kataane changed the title Cleanup and improve OnlyExplicitMappedMembers implementation Add OnlyExplicitMappedMembers implementation Oct 23, 2025
Copy link
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 your contribution, I added my feedback.

Comment on lines +187 to +189
var target = new global::B();
target.Value1 = source.Value1;
target.Value2 = source.Value2;
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the description on the configuration member:

When set to true, only properties with explicit configurations (via attributes like MapProperty) will be mapped. All other properties will be ignored by default.

all members should be ignored, even if the match exactly, except if they are mapped explicitly. This isn't the case here and in the other tests, is it?

}

[Fact]
public void OnlyExplicitMappedMembersWithExtraSourceMembers()
Copy link
Contributor

Choose a reason for hiding this comment

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

Also add tests with explicit mappings (MapProperty, MapPropertyFromSource, ...)

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

/// <summary>
/// When set to <c>true</c>, only properties with explicit configurations (via attributes like <c>MapProperty</c>)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use see tag for the MapProperty attribute

@latonz
Copy link
Contributor

latonz commented Dec 25, 2025

Closing for now as no update was received for several weeks... Feel free to reopen with updates.

@latonz latonz closed this Dec 25, 2025
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.

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

2 participants