-
-
Notifications
You must be signed in to change notification settings - Fork 215
fix: generic and runtime target type mappings with derived types #2230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,10 +91,16 @@ public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext c | |
|
|
||
| protected virtual ExpressionSyntax? BuildSwitchArmWhenClause(ExpressionSyntax runtimeTargetType, RuntimeTargetTypeMapping mapping) | ||
| { | ||
| // targetType.IsAssignableFrom(typeof(ADto)) | ||
| var mappingTargetType = TypeOfExpression(FullyQualifiedIdentifier(mapping.Mapping.TargetType.NonNullable())); | ||
|
|
||
| // Derived type mapping: A => ADto (runtime target type) or A => BaseDto (runtime target type), BaseDto (mapping target type). | ||
| // typeof(BaseDto).IsAssignableFrom(runtimeTargetType) | ||
|
|
||
| // Non-derived type mapping: A => ADto (runtime target type) or A => BaseDto (runtime target type), ADto (mapping target type). | ||
| // runtimeTargetType.IsAssignableFrom(ADto) | ||
| return InvocationWithoutIndention( | ||
| MemberAccess(runtimeTargetType, IsAssignableFromMethodName), | ||
| TypeOfExpression(FullyQualifiedIdentifier(mapping.Mapping.TargetType.NonNullable())) | ||
| MemberAccess(mapping.IsDerivedTypeMapping ? mappingTargetType : runtimeTargetType, IsAssignableFromMethodName), | ||
| mapping.IsDerivedTypeMapping ? runtimeTargetType : mappingTargetType | ||
|
Comment on lines
+102
to
+103
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably is a breaking change, isn't it? Document it in the breaking change docs. |
||
| ); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -338,7 +338,8 @@ string sourceParameterName | |
| parameters.Source, | ||
| parameters.ReferenceHandler, | ||
| ctx.SymbolAccessor.UpgradeNullable(methodSymbol.ReturnType), | ||
| ctx.Configuration.Mapper.UseReferenceHandling | ||
| ctx.Configuration.Mapper.UseReferenceHandling, | ||
| ctx.AttributeAccessor.HasAttribute<MapDerivedTypeAttribute<object, object>>(methodSymbol) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO the correct location to set this would be |
||
| ) | ||
| { | ||
| AdditionalSourceParameters = parameters.AdditionalParameters, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,7 +165,7 @@ public void WithDerivedTypesShouldUseBaseType() | |
| """ | ||
| return source switch | ||
| { | ||
| global::Base x when targetType.IsAssignableFrom(typeof(global::BaseDto)) => MapDerivedTypes(x), | ||
| global::Base x when typeof(global::BaseDto).IsAssignableFrom(targetType) => MapDerivedTypes(x), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a new test case reflecting the exact use-case from the referenced issue. |
||
| _ => throw new global::System.ArgumentException($"Cannot map {source.GetType()} to {targetType} as there is no known type mapping", nameof(source)), | ||
| }; | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer pattern matching
x is UserDefinedNewInstanceMethodMapping { IsDerivedTypeMapping: true }