Skip to content

Fix optional request body to wrap Content-Type header in null check#10079

Merged
jorgerangel-msft merged 5 commits intomainfrom
copilot/support-optional-request-body
Mar 19, 2026
Merged

Fix optional request body to wrap Content-Type header in null check#10079
jorgerangel-msft merged 5 commits intomainfrom
copilot/support-optional-request-body

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

When the request body is optional, the C# generator was unconditionally setting the Content-Type header, producing invalid code like if ("application/xml" != null). The Content-Type header should be gated on the content parameter being non-null. The request.Content assignment does not need wrapping since setting it to null is a no-op.

Before:

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

After:

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

Changes

  • RestClientProvider.AppendHeaderParameters: Reordered null check conditions so the Content-Type + optional body check runs before the general header null check. This fixes an edge case where an optional Content-Type header (contentType?: "application/xml") matched the general null check first, wrapping with the literal value instead of the content parameter.
  • Tests: Rewrote ContentTypeHeaderWrappedInNullCheckWhenContentIsOptional and ContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequired to use TestData pattern with MockClientProvider + TypeProviderWriter + Helpers.GetExpectedFromFile(), comparing the full generated CreateRequest method against expected output files. Added ContentTypeHeaderWrappedInNullCheckWhenContentTypeIsOptional test covering the edge case where the Content-Type header itself is optional.
  • TestData: Added ContentTypeHeaderWrappedInNullCheckWhenContentIsOptional.cs, ContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequired.cs, and ContentTypeHeaderWrappedInNullCheckWhenContentTypeIsOptional.cs TestData files showing the isolated CreateRequest method output for each scenario
Original prompt

This section details on the original issue you should resolve

<issue_title>Support Optional Request Body & Optional Content Type</issue_title>
<issue_description>Consider the following spec : link

Here the request body and the content type header are optional. The content type header should only be added to the request if the request body is not empty. Currently, we generate invalid code that will always add the content type parameter:

if ("application/xml" != null)
{
    request.Headers.SetValue("Content-Type", "application/xml");
}

Instead, we should check if the request content is null, and if not, apply the content type:

if (content != null)
{
    request.Headers.SetValue("Content-Type", "application/xml");
    request.Content = content;
}
```</issue_description>

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

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…r optional body

When the request body is optional, both the Content-Type header setting and
the request.Content assignment are now wrapped in an if (content != null)
block. This prevents setting Content-Type when no body is provided.

For optional body without Content-Type header, the content assignment
alone is wrapped in a null check.

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-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 Mar 18, 2026
@jorgerangel-msft
Copy link
Contributor

@copilot for context, this was suppose to be fixed in a previous change #9521.

Copilot AI changed the title [WIP] Add support for optional request body and content type Fix optional request body to wrap Content-Type and content assignment in null check Mar 18, 2026
Copilot AI requested a review from jorgerangel-msft March 18, 2026 23:25
Copy link
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot for context, this was suppose to be fixed in a previous change #9521.

Thanks for the context. PR #9521 wrapped the Content-Type header in if (content != null) but left request.Content = content unconditionally outside that block. This PR (commit 47ad15a) extends that fix by moving the content assignment inside the same null check, so both the header and content are only set when the body is present.

Copilot AI and others added 2 commits March 18, 2026 23:41
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…ew feedback

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot AI changed the title Fix optional request body to wrap Content-Type and content assignment in null check Fix optional request body to wrap Content-Type header in null check Mar 18, 2026
Copilot AI requested a review from jorgerangel-msft March 18, 2026 23:52
…tent null check

Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
@jorgerangel-msft jorgerangel-msft added this pull request to the merge queue Mar 19, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10079

commit: 0a5a7da

Merged via the queue into main with commit b6db3b8 Mar 19, 2026
25 checks passed
@jorgerangel-msft jorgerangel-msft deleted the copilot/support-optional-request-body branch March 19, 2026 17:57
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.

Support Optional Request Body & Optional Content Type

3 participants