Skip to content

Handle collection-backed C# error messages - #8013

Open
AayushP123 wants to merge 1 commit into
microsoft:mainfrom
AayushP123:agent/fix-7926-csharp-collection-error-message
Open

Handle collection-backed C# error messages#8013
AayushP123 wants to merge 1 commit into
microsoft:mainfrom
AayushP123:agent/fix-7926-csharp-collection-error-message

Conversation

@AayushP123

Copy link
Copy Markdown
Contributor

Summary

Fixes #7926.

The primary error-message path builder treated collection properties like single model properties. For errors such as List<ProcessResult> ProcessResults, that produced ProcessResults?.MessageEscaped, which does not compile.

Changes

  • Allow language writers to customize a primary-message path segment based on whether the traversed type is a collection.
  • Use FirstOrDefault() when the C# error-message path crosses a collection.
  • Add System.Linq only to affected C# error models.
  • Cover both the generated property expression and conditional import refinement.

Testing

  • dotnet test tests/Kiota.Builder.Tests/Kiota.Builder.Tests.csproj --filter "FullyQualifiedName~Writers.CSharp.CodePropertyWriterTests|FullyQualifiedName~Refiners.CSharpLanguageRefinerTests|FullyQualifiedName~Writers.ProprietableBlockExtensions" --no-restore
  • dotnet 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
  • Generated the full attached Dayforce client and built it against Kiota abstractions and serializers with zero warnings and zero errors.

Copilot AI review requested due to automatic review settings August 1, 2026 08:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Linq only 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
AayushP123 force-pushed the agent/fix-7926-csharp-collection-error-message branch from aec9d9a to 1052b7e Compare August 1, 2026 09:10
@AayushP123
AayushP123 marked this pull request as ready for review August 2, 2026 17:08
@AayushP123
AayushP123 requested a review from a team as a code owner August 2, 2026 17:08
Copilot AI review requested due to automatic review settings August 2, 2026 17:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

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.

[C#] Invalid code generated for the primary error message when it is carried by a collection property (List<T>)

2 participants