Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Fix Content-Type header setting in RestClient ✅

Summary

Fixed an issue where Content-Type headers were set unconditionally, even when request content was optional and might be null.

Changes Made

  • Modified RestClientProvider.BuildCreateRequestMethodBody to detect and pass content parameter to AppendHeaderParameters
  • Updated AppendHeaderParameters to wrap Content-Type header setting in if (content != null) check only when body parameter is optional
  • Added comprehensive tests to verify the fix with full statement assertion
  • Added test for required body parameter with assertion that Content-Type is NOT wrapped in if statement
  • Removed unnecessary cast - ParameterProvider has implicit conversion to ValueExpression
  • All 51 RestClientProvider tests passing

Result

Before:

// When body is optional
request.Headers.Set("Content-Type", "application/json");
request.Content = content;  // content could be null

After (when body is optional):

if (content != null)
{
    request.Headers.Set("Content-Type", "application/json");
}
request.Content = content;

After (when body is required):

request.Headers.Set("Content-Type", "application/json");
request.Content = content;  // content is required, no null check needed

This ensures Content-Type headers are only wrapped in null checks when the request body parameter is actually optional, avoiding unnecessary checks for required parameters.

Original prompt

This section details on the original issue you should resolve

<issue_title>Content-Type setting in RestClient is not correct</issue_title>
<issue_description>Currently we have code generated like this Azure/azure-sdk-for-net#55347 (comment). This isn't correct (obviously). We should instead be checking if the RequestContent is null when setting the Content-Type header.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jan 29, 2026
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Content-Type setting in RestClient Fix Content-Type header set when request content is null Jan 29, 2026
Copilot AI requested a review from JoshLove-msft January 29, 2026 23:31
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title Fix Content-Type header set when request content is null Fix Content-Type header set when request content is optional Jan 30, 2026
Copilot AI requested a review from JoshLove-msft January 30, 2026 00:40
…ed body

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Jan 30, 2026
Merged via the queue into main with commit ccf99a4 Jan 30, 2026
23 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/fix-content-type-header branch January 30, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Content-Type setting in RestClient is not correct

3 participants