Skip to content

Fix: MapAtRuntime exception with nullable target types and extension methods (AutoMapper #4597 backport)#10

Closed
gthen95 wants to merge 1 commit intoNET-Sorcery:mainfrom
gthen95:fix/map-at-runtime-nullable
Closed

Fix: MapAtRuntime exception with nullable target types and extension methods (AutoMapper #4597 backport)#10
gthen95 wants to merge 1 commit intoNET-Sorcery:mainfrom
gthen95:fix/map-at-runtime-nullable

Conversation

@gthen95
Copy link
Contributor

@gthen95 gthen95 commented Mar 19, 2026

Description

This PR backports a fix from AutoMapper (#4597) to resolve a runtime exception when combining .MapAtRuntime() with extension methods targeting a nullable type.

The Issue:
When using .MapAtRuntime() alongside .MapFrom(src => src.Value.ExtensionMethod()) where the destination type is nullable (e.g., int?), the mapping throws a runtime exception. The expression builder passes the source parameter directly without explicitly converting it to the expected type of the generic expression.

The Fix:
Added an explicit ToType() (or Expression.Convert) cast to the source parameter inside ContextMap before passing it to the Expression.Call. This ensures type safety and prevents the runtime crash.

References:

@kant2002
Copy link
Collaborator

Thank you for your initiative. Unfortunately I cannot accept your contributions, since they are seems to be just rehashed work of @jbogard which is bad sport. I understand that this is trivial change, but when compounded it can be legal issue. As you may notice this repository still have MIT license, but original AutoMapper is not. You may try to go to authors of PR's and ask them to confirm that their change is under MIT. anyway you should keep authorship intact. If you want attribution, I think that can be handled with Co-authored or something similar. Anyway PR probably would count as such.

@gthen95
Copy link
Contributor Author

gthen95 commented Mar 19, 2026

Ok, I understand

@gthen95 gthen95 closed this Mar 19, 2026
@gthen95 gthen95 deleted the fix/map-at-runtime-nullable branch March 19, 2026 13:53
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.

MapAtRuntime with with non-nullable to nullable mapping fails when extension methods are used

2 participants