[http-client-csharp] Fix spread model parameter matching to use wire name#10111
Merged
JoshLove-msft merged 4 commits intoMar 23, 2026
Merged
Conversation
commit: |
Contributor
|
No changes needing a change description found. |
When constructing spread models in convenience methods, the parameter matching between convenience method parameters and model constructor parameters used C# names which can diverge due to: - @clientName renames (e.g., 'model' -> 'OverrideModelName') - @Encodedname wire names leaking (e.g., 'tool_resources' vs 'toolResources') - PascalCase vs camelCase differences This caused unmatched parameters to fall back to 'default' (null), leading to NullReferenceException during serialization when Optional.IsCollectionDefined(null) returns true for null collections. Fix: match by the property's wire (serialized) name, which is stable across both the convenience parameter and the model property since they represent the same JSON field. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
875c063 to
6eb86ca
Compare
…hing Move the TryGetValue into the if condition directly so the compiler can track that convenienceParam is definitely assigned when used. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jorgerangel-msft
approved these changes
Mar 23, 2026
The Sample-TypeSpec test project now correctly forwards the 'name' parameter in spread model construction instead of using 'default'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The wire-name-only matching fails for cases where the constructor parameter's Property.WireInfo is not available (e.g., in unit tests using mock models). Add a fallback that matches by C# name (case-insensitive) when wire name matching doesn't find a match. Matching order: 1. Wire name (handles @clientName, @Encodedname, casing differences) 2. C# name (handles simple cases and parameters without wire info) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
de5193d to
a65a97c
Compare
jorgerangel-msft
approved these changes
Mar 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.
Problem
When generating convenience methods with spread model construction, parameters were matched between the convenience method and the model constructor by C# name. This fails when:
Unmatched parameters fell back to \default\ (null), causing \NullReferenceException\ during serialization when \Optional.IsCollectionDefined(null)\ returns \ rue\ for null collections.
This was discovered while migrating \Azure.AI.Agents.Persistent\ to the new @azure-typespec/http-client-csharp\ emitter — 172 out of 261 tests failed with NRE in serialization.
Fix
Match spread parameters by their wire (serialized) name (\WireInfo.SerializedName) instead of C# name. Both the convenience parameter and model constructor parameter share the same wire name since they represent the same JSON field. Uses \TryAdd\ to handle potential duplicate wire names gracefully.
Validation