Fix optional request body to wrap Content-Type header in null check#10079
Merged
jorgerangel-msft merged 5 commits intomainfrom Mar 19, 2026
Merged
Fix optional request body to wrap Content-Type header in null check#10079jorgerangel-msft merged 5 commits intomainfrom
jorgerangel-msft merged 5 commits intomainfrom
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>
Contributor
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
Contributor
Author
Thanks for the context. PR #9521 wrapped the Content-Type header in |
...sharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/RestClientProvider.cs
Outdated
Show resolved
Hide resolved
...TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/RestClientProviderTests.cs
Outdated
Show resolved
Hide resolved
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
...TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/RestClientProviderTests.cs
Show resolved
Hide resolved
…tent null check Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
JoshLove-msft
approved these changes
Mar 19, 2026
commit: |
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.
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
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.ContentTypeHeaderWrappedInNullCheckWhenContentIsOptionalandContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequiredto use TestData pattern withMockClientProvider+TypeProviderWriter+Helpers.GetExpectedFromFile(), comparing the full generated CreateRequest method against expected output files. AddedContentTypeHeaderWrappedInNullCheckWhenContentTypeIsOptionaltest covering the edge case where the Content-Type header itself is optional.ContentTypeHeaderWrappedInNullCheckWhenContentIsOptional.cs,ContentTypeHeaderNotWrappedInNullCheckWhenContentIsRequired.cs, andContentTypeHeaderWrappedInNullCheckWhenContentTypeIsOptional.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.