Fix potential NullReferenceException in C# union model discriminator generation#7641
Merged
baywet merged 7 commits intomicrosoft:mainfrom Apr 23, 2026
Merged
Conversation
…generation When no discriminator mapping is found for a property type, `FirstOrDefault` returns a default KeyValuePair with a null key. Accessing `mappedType.Key` without a null check could generate invalid C# code with empty discriminator strings. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
7846692 to
2747093
Compare
adrian05-ms
reviewed
Apr 22, 2026
Contributor
adrian05-ms
left a comment
There was a problem hiding this comment.
Thank you for the contribution! Some of the integration tests are failing. Please fix them and I'll approve and merge the PR
There was a problem hiding this comment.
Pull request overview
This PR hardens union-model factory-method generation against missing discriminator mappings by avoiding code emission when the discriminator mapping key can’t be resolved, preventing writer-time exceptions and malformed conditional chains across multiple language emitters.
Changes:
- Add null/empty guards around discriminator mapping keys before emitting discriminator-based branches in union factory methods (C#, Python, Dart; skip/continue in Go/Java/PHP).
- Fix conditional-chain state (
includeElse) and closing-brace behavior to avoid emitting invalidelse if/dangling blocks when no discriminator branches are generated. - Update
CHANGELOG.mdunder Unreleased to document the fix (but currently includes unrelated entries).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs | Skip discriminator branch generation when the mapping key can’t be found; keep else chaining valid. |
| src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs | Guard mapped discriminator key usage and ensure elif chaining only begins after an emitted branch. |
| src/Kiota.Builder/Writers/Dart/CodeMethodWriter.cs | Avoid reading discriminator unless needed; skip branches with missing mapping keys and fix else chaining. |
| src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs | Skip discriminated-type branches when mapping key is null/empty; only close the chain when started. |
| src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs | Skip discriminated-type branches when mapping key is null/empty; only close the chain when started. |
| src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs | Skip discriminated-type branches when mapping key is null/empty; only close the chain when started. |
| CHANGELOG.md | Add an Unreleased changelog entry for the fix (currently also adds unrelated items). |
Remove unrelated CHANGELOG entries (microsoft#7643, microsoft#7642, microsoft#7639) that don't belong to this PR. Add C# and Dart writer tests verifying that union model factory methods skip discriminator branches when the mapping key is null/empty (i.e. an unmapped complex type). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
auto-merge was automatically disabled
April 23, 2026 12:46
Head branch was pushed to by a user without write access
adrian05-ms
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mappedType.Keybefore generating discriminator condition in C# union model factory methodProblem
In
WriteFactoryMethodBodyForUnionModel, when iterating over properties of a union/discriminated type:If
FirstOrDefaultfinds no matching mapping, it returnsdefault(KeyValuePair<string, CodeType>)with anullkey. The next line unconditionally accessesmappedType.Key, generating invalid C# code like"".Equals(mappingValue, ...)instead of a proper discriminator check.Fix
Guard the
WriteLinecall with a null/empty check:Test plan
🤖 Generated with Claude Code