fix: generic and runtime target type mappings with derived types#2230
Merged
Conversation
justi7n
force-pushed
the
fix/polymorphic-mappings
branch
from
April 13, 2026 11:53
a7e108c to
32ee630
Compare
latonz
requested changes
Apr 13, 2026
Contributor
Author
|
Example for clarification: Lets assume we have BananaDto : FruitDto, and FruitDto : OrganicDto. The previously released code in public partial class DerivedTypeMapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.1.0")]
public partial T MapGeneric<T>(object source)
{
return source switch
{
// --- Relevant Part ---
global::MapperlyRepro.Fruit x when typeof(T).IsAssignableFrom(typeof(global::MapperlyRepro.FruitDto)) => (T)(object)MapDerivedTypes(x),
_ => throw new global::System.ArgumentException($"Cannot map {source.GetType()} to {typeof(T)} as there is no known type mapping", nameof(source)),
};
}
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.1.0")]
public partial global::MapperlyRepro.FruitDto MapDerivedTypes(global::MapperlyRepro.Fruit source)
{
return source switch
{
global::MapperlyRepro.Banana x => MapToBananaDto(x),
_ => throw new global::System.ArgumentException($"Cannot map {source.GetType()} to MapperlyRepro.FruitDto as there is no known derived type mapping", nameof(source)),
};
}
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.1.0")]
private global::MapperlyRepro.BananaDto MapToBananaDto(global::MapperlyRepro.Banana source)
{
...
}
}by [Mapper]
public partial class DerivedTypeMapper
{
public partial T MapGeneric<T>(object source);
[MapDerivedType<Banana, BananaDto>]
public partial FruitDto MapDerivedTypes(Fruit source);
}So previously, the mapping would work for:
If we reverse the AssignableFrom argument order (like currently in this PR), we would reverse the order of what would work:
So previously, if someone called |
justi7n
force-pushed
the
fix/polymorphic-mappings
branch
2 times, most recently
from
June 12, 2026 11:01
890f09f to
73028da
Compare
justi7n
force-pushed
the
fix/polymorphic-mappings
branch
from
June 12, 2026 11:10
73028da to
adbacf4
Compare
latonz
approved these changes
Jun 19, 2026
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.
Fix generic and runtime target type mappings with derived types
Description
If a user defined generic type or runtime target type mapping has a child mapping method which uses the
[MapDerivedType]attribute, the parent method switch arm when clause was created wrongly (but only in that specific case). Fixes #1989This got fixed in this PR
Fixes # (issue)
Checklist