Skip to content

Conversation

@Kataane
Copy link
Contributor

@Kataane Kataane commented Jan 2, 2026

Fix the handling of nullable types in the NullDelegateMapping.

Description

Hi @latonz.

This pull request (PR) addresses an issue with nullable type handling in the NullDelegateMapping class. It ensures that, when both the source and target types are nullable and have the same underlying type, the delegate mapping is built properly without unnecessary casts or null checks.

Key changes:

  • Special case handling was added in NullDelegateMapping.cs for nullable source and target types with identical underlying types.
  • Simplified dictionary value mapping by removing unnecessary null checks in the generated code.
  • Added comprehensive test coverage for nullable array mappings.
  • Test snapshots were updated to reflect the simplified generated code.

Technical details:
The fix adds a condition to NullDelegateMapping.Build() that checks if both the source and target types are nullable and share the same underlying type. When this condition is met, it delegates to the underlying mapping without additional null handling, resulting in cleaner generated code.

Example of improvement:
Before: target[item.Key] = item.Value == null ? default : item.Value;
After: target[item.Key] = item.Value;

related #1614

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)

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 this contribution! 🥳

@latonz latonz changed the title Fix the handling of nullable types in the NullDelegateMapping. fix: don't emit unnecessary null-handling Jan 5, 2026
@latonz latonz changed the title fix: don't emit unnecessary null-handling fix: don't emit unnecessary null-handling if source and target are nullable Jan 5, 2026
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.

2 participants