Guard exploded string-struct query parameters against null ToString in C# emitter - #11439
Guard exploded string-struct query parameters against null ToString in C# emitter#11439jorgerangel-msft with Copilot wants to merge 7 commits into
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
… emitter Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
commit: |
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime crash and a serialization bug in the C# HTTP client emitter when generating exploded query parameters for string-backed extensible enums (struct enums). It ensures null/empty enum string values aren’t passed into AppendQuery(..., escape: true) and corrects dictionary explode serialization to use KeyValuePair.Value rather than the KeyValuePair itself.
Changes:
- Add
string.IsNullOrEmpty(...)guards around per-element/per-valueAppendQuerycalls for exploded arrays/dictionaries when the element type is a string-backed extensible enum. - Fix dictionary explode enum serialization to serialize
item.Value(not theKeyValuePair), correcting generated output for both string and numeric enum dictionary values. - Extend
RestClientProviderTests.TestBuildCreateRequestMethodWithQueryParametersand update the expected baseline to cover extensible string enum exploded array/dictionary cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/RestClientProvider.cs | Guards exploded query serialization for extensible string enums and fixes dictionary explode value serialization. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/RestClientProviderTests.cs | Adds new test inputs for extensible string enum exploded array/dictionary query parameters. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/TestData/RestClientProviderTests/TestBuildCreateRequestMethodWithQueryParameters.cs | Updates expected generated output to include null/empty guards and corrected dictionary value serialization. |
…explode query params Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…y and shared helper Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…ble enum query param test Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…l guard validation Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
| if (IsExtensibleStringEnum(elementType)) | ||
| { | ||
| forEachStatement.Add(Declare("paramStr", typeof(string), convertedItem, out VariableExpression cachedVar)); | ||
| forEachStatement.Add(new IfStatement(Not(StringSnippets.IsNullOrEmpty(cachedVar.As<string>()))) |
There was a problem hiding this comment.
This guard also filters out an explicitly constructed empty-string enum value, not just the default struct's null backing value. The generated extensible-enum constructor rejects null but accepts "", and AppendQuery(..., "", true) safely represents that value as an empty query entry; after this change an array/dictionary element created from "" is silently omitted instead. Could this check only for null and add coverage for an empty-string enum element/value?
--generated by Copilot
Exploded
@queryarray/dictionary parameters backed by a string struct (extensible string enum) generated code that passed.ToString()directly toAppendQuery(..., escape: true). A default-constructed value has a null internal string, so.ToString()returns null andUri.EscapeDataStringthrowsArgumentNullExceptionat runtime.Changes
RestClientProvider.BuildAppendQueryStatement): wraps eachAppendQueryinif (!string.IsNullOrEmpty(...))for array and dictionary explode when the element/value is a string-backed extensible enum. Gated by a newIsExtensibleStringEnumhelper (IsEnum && IsStruct && UnderlyingEnumType == typeof(string)) so int/float/double enums and plain values are unaffected.KeyValuePair(item) rather thanitem.Value, yielding wrong output (e.g.@param.ToSerialString(),((int)@param)). Now usesitem.Value, correcting the p6/p7 paths as well.TestBuildCreateRequestMethodWithQueryParameterswith extensible string enum array + dictionary parameters; baseline updated.Generated output now:
ToString()when serializing exploded query parameters backed by string structs #11431