Skip to content

fix: cast inherited MapWith results to derived destinations (#947)#976

Open
leno23 wants to merge 1 commit into
MapsterMapper:developmentfrom
leno23:fix/inherit-mapwith-derived-dest-947-dev
Open

fix: cast inherited MapWith results to derived destinations (#947)#976
leno23 wants to merge 1 commit into
MapsterMapper:developmentfrom
leno23:fix/inherit-mapwith-derived-dest-947-dev

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 25, 2026

Summary

Fixes #947.

When AllowImplicitDestinationInheritance is enabled and a base mapping uses MapWith (returning the base destination type), calling Adapt<TDerived>() previously compiled a converter whose return type was the base type, causing an InvalidCastException at runtime even when the factory actually created a derived instance.

This adjusts the compiled converter to downcast the MapWith result to the requested derived destination type.

Changes

  • TypeAdapterConfig.CreateMapExpression: add AdjustInheritedConverterReturnType to cast inherited MapWith results when the configured return type is a base of the requested destination.
  • Add regression tests covering Adapt<Dog>() and Adapt<AnimalDto, Dog>() for the reported scenario.

Test plan

  • CI passes on development
  • New tests in WhenImplicitInheritanceMapWithDerivedDestination pass
  • Existing implicit inheritance tests remain green

Made with Cursor

…apper#947)

When AllowImplicitDestinationInheritance merges a base MapWith converter
into a derived destination mapping, downcast the converter result so
Adapt<TDerived>() no longer throws InvalidCastException.

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

1 participant