Skip to content

fix: use null-conditional access for nested source in existing target mappings - #2498

Open
EnesKartal wants to merge 1 commit into
riok:mainfrom
EnesKartal:fix/2303-nested-readonly-collection-null-check
Open

EnesKartal wants to merge 1 commit into
riok:mainfrom
EnesKartal:fix/2303-nested-readonly-collection-null-check

Conversation

@EnesKartal

Copy link
Copy Markdown

Description

When mapping a nested source member onto an existing target member (such as a readonly collection), MemberExistingTargetMapping previously built a non-null-conditional source access (source.Nested.Items) and passed it directly into NullDelegateExistingTargetMapping. When an intermediate parent in the source path was null, the generated guard if (source.Nested.Items != null && target.Items != null) threw a NullReferenceException (and could trigger CS8602 when nullable reference types are enabled).

This change:

  • Adds a Build overload on NullDelegateExistingTargetMapping that accepts a null-safe source expression for the null guard while using the unwrapped source access inside the guarded body.
  • Updates MemberExistingTargetMapping.Build to use null-conditional access (e.g., source.Nested?.Items != null) when any parent in the source member path is nullable, handles nullable value-type parents properly, and guards non-nullable delegate mappings.
  • Adds unit tests in EnumerableExistingTargetTest covering disabled nullable context, nullable reference/struct parents, value-type enumerables, and user-implemented existing target methods.

Fixes #2303

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Unit tests are added/updated

This branch has not been deployed

No deployments
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.

Mapping a nested list onto a readonly list can lead to nullreference exception when the source list is null.

1 participant