Fix optional request body to wrap Content-Type header in null check#10079
Fix optional request body to wrap Content-Type header in null check#10079
Conversation
…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>
Thanks for the context. PR #9521 wrapped the Content-Type header in |
| var contentParam = parameters.FirstOrDefault( | ||
| p => p.Location == ParameterLocation.Body); | ||
| return contentParam is null ? [] : [request.Content().Assign(contentParam).Terminate()]; | ||
| if (contentParam is null) |
There was a problem hiding this comment.
@copilot actually, we can simplify this. Instead of generating:
if (content != null)
{
request.Headers.SetValue("Content-Type", "application/xml");
request.Content = content;
}Lets simply conditionally add the content type header:
if (content != null)
{
request.Headers.SetValue("Content-Type", "application/xml");
}The request.Content property can be both null or not, so conditionally checking it is a no-op
There was a problem hiding this comment.
Reverted the source changes in 067ed6f. RestClientProvider.cs is back to the PR #9521 behavior — only the Content-Type header is wrapped in if (content != null), and request.Content = content stays unconditional.
| var statements = createMethod!.BodyStatements as MethodBodyStatements; | ||
| Assert.IsNotNull(statements); | ||
|
|
||
| var expectedStatement = @"if ((content != null)) |
There was a problem hiding this comment.
@copilot lets use TestData to compare the generated CreateRequest method. We should filter out all the other generated methods to isolate the generated code for the tests
There was a problem hiding this comment.
Done in 067ed6f. Both tests now use MockClientProvider + TypeProviderWriter + Helpers.GetExpectedFromFile() with TestData files that contain the isolated CreateRequest method output.
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>
When the request body is optional, the C# generator was unconditionally setting the
Content-Typeheader, producing invalid code likeif ("application/xml" != null). The Content-Type header should be gated on the content parameter being non-null. Therequest.Contentassignment does not need wrapping since setting it to null is a no-op.Before:
After:
Changes
ContentTypeHeaderWrappedInNullCheckWhenContentIsOptionalandContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequiredto use TestData pattern withMockClientProvider+TypeProviderWriter+Helpers.GetExpectedFromFile(), comparing the full generated CreateRequest method against expected output filesContentTypeHeaderWrappedInNullCheckWhenContentIsOptional.csandContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequired.csTestData files showing the isolated CreateRequest method output for each scenarioOriginal prompt
💡 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.