Handle collection-backed C# error messages - #8013
Open
AayushP123 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes C# code generation for ApiException.Message overrides when the primary error message is reached through a collection property (e.g., List<T>), which previously produced invalid member-access code that did not compile.
Changes:
- Extends the primary-error-message path builder to allow language-specific segment customization based on whether the traversed type is a collection.
- Updates the C# writer to insert
FirstOrDefault()when the primary-message path crosses a collection, producing compilable null-safe access. - Updates the C# refiner to add
System.Linqonly for affected error models and adds/updates unit tests covering both the generated expression and the conditional import.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs | Adds coverage for Message override generation when the primary message is nested behind a collection property. |
| tests/Kiota.Builder.Tests/Refiners/CSharpLanguageRefinerTests.cs | Adds coverage ensuring System.Linq is imported only when FirstOrDefault() is required by the generated primary-message path. |
| src/Kiota.Builder/Writers/ProprietableBlockExtensions.cs | Adds an optional pathSegmentFactory to customize path segments per traversed type (e.g., collection vs non-collection). |
| src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs | Uses pathSegmentFactory to emit ?.FirstOrDefault()?. when the primary-message path crosses a collection in C#. |
| src/Kiota.Builder/Refiners/CSharpRefiner.cs | Conditionally adds System.Linq for error models whose primary-message path requires FirstOrDefault(). |
| CHANGELOG.md | Documents the C# behavior change for primary error messages nested in collections. |
AayushP123
force-pushed
the
agent/fix-7926-csharp-collection-error-message
branch
from
August 1, 2026 09:10
aec9d9a to
1052b7e
Compare
AayushP123
marked this pull request as ready for review
August 2, 2026 17:08
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
Fixes #7926.
The primary error-message path builder treated collection properties like single model properties. For errors such as
List<ProcessResult> ProcessResults, that producedProcessResults?.MessageEscaped, which does not compile.Changes
FirstOrDefault()when the C# error-message path crosses a collection.System.Linqonly to affected C# error models.Testing
dotnet test tests/Kiota.Builder.Tests/Kiota.Builder.Tests.csproj --filter "FullyQualifiedName~Writers.CSharp.CodePropertyWriterTests|FullyQualifiedName~Refiners.CSharpLanguageRefinerTests|FullyQualifiedName~Writers.ProprietableBlockExtensions" --no-restoredotnet test tests/Kiota.Builder.Tests/Kiota.Builder.Tests.csproj --filter FullyQualifiedName!~KiotaSearcherTests --no-restore(2,185 passed, 2 skipped)dotnet format kiota.slnx --include src/Kiota.Builder/Refiners/CSharpRefiner.cs src/Kiota.Builder/Writers/CSharp/CodePropertyWriter.cs src/Kiota.Builder/Writers/ProprietableBlockExtensions.cs tests/Kiota.Builder.Tests/Refiners/CSharpLanguageRefinerTests.cs tests/Kiota.Builder.Tests/Writers/CSharp/CodePropertyWriterTests.cs --no-restore --verify-no-changes