Skip to content

Fix body optionality for contentType and request body setting #45528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2025

Conversation

samvaity
Copy link
Member

Description

  • Adds support for DateTimeFormatter.ISO_INSTANT for OffsetDateTime header and query parameters
  • Fixes body optionality for contentType and request body setting (only set content type if body is not null)
  • Fixes setting request body according to provided body content type if isJson.

Copy link
Contributor

github-actions bot commented May 29, 2025

API Change Check

APIView identified API level changes in this PR and created the following API reviews

io.clientcore:annotation-processor

@samvaity samvaity marked this pull request as ready for review May 30, 2025 03:33
@Copilot Copilot AI review requested due to automatic review settings May 30, 2025 03:33
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances how request bodies and headers are handled by:

  • Adding ISO_INSTANT formatting support for OffsetDateTime in query and header parameters
  • Only setting the Content-Type header when a non-null body is present and choosing serialization based on JSON vs. other types
  • Updating generated client templates and updating/removing legacy tests to reflect the new body‐optional behavior

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
sdk/clientcore/core/src/main/java/io/clientcore/core/utils/GeneratedCodeUtils.java Added OffsetDateTime query‐parameter formatting support
sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/RequestBodyHandler.java Refactored configureRequestBody to honor body optionality and JSON
sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/templating/JavaParserTemplateProcessor.java Updated template logic for body header handling and skip content-type
sdk/clientcore/annotation-processor/pom.xml Lowered Jacoco coverage thresholds
sdk/clientcore/annotation-processor/src/test/java/io/clientcore/annotation/processor/utils/RequestBodyHandlerTest.java Removed outdated RequestBodyHandler tests
sdk/clientcore/annotation-processor-test/src/test/java/io/clientcore/annotation/processor/test/serializers/XmlSerializableTests.java Tweaked XML tests to propagate new header logic
sdk/clientcore/annotation-processor-test/src/test/java/io/clientcore/annotation/processor/test/TestInterfaceServiceClientGenerationTests.java Adjusted JSON body expectations (now quoted)
sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/implementation/TestInterfaceClientServiceImpl.java Regenerated client impls to only set Content-Type when body exists
sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/implementation/SpecialReturnBodiesService.java Added new rfc3339 and omit operations to service interface
sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/SpecialReturnBodiesServiceImpl.java Implemented new rfc3339 and omit behaviors with ISO_INSTANT
sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/SimpleXmlSerializableServiceImpl.java Moved XML header setup inside null check
Comments suppressed due to low confidence (3)

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/RequestBodyHandler.java:51

  • [nitpick] The variable name contentTypeParamParamOpt is verbose and contains a repeated 'Param'. Consider renaming it to contentTypeParamOpt for clarity.
Optional<HttpRequestContext.MethodParameter> contentTypeParamParamOpt

sdk/clientcore/annotation-processor/src/test/java/io/clientcore/annotation/processor/utils/RequestBodyHandlerTest.java:1

  • Removing the RequestBodyHandlerTest leaves new request-body logic untested. Add replacement tests to cover cases where Content-Type is only set when the body is non-null.
Entire test file removed

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/utils/RequestBodyHandler.java:247

  • Switching to always JSON‐serialize non-null bodies introduces quoting for string payloads (including empty strings). This is a behavioral change—ensure it’s documented as a breaking change in release notes.
if (bodyContentType != null && bodyContentType.trim().equalsIgnoreCase(ContentType.APPLICATION_JSON)) {

@samvaity samvaity merged commit d9d35fc into Azure:main May 30, 2025
59 checks passed
@@ -457,6 +441,10 @@ private void addHeadersToRequest(BlockStmt body, HttpRequestContext method) {
// Start building the header addition for the HttpRequest.
StringBuilder addHeader = new StringBuilder();

String constantName = LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT.get(headerKey.toLowerCase(Locale.ROOT));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo in the name - LOWERCASE_HEADER_TO_HTTPHEADERNAME_CONSTANT

@@ -457,6 +441,10 @@ private void addHeadersToRequest(BlockStmt body, HttpRequestContext method) {
// Start building the header addition for the HttpRequest.
StringBuilder addHeader = new StringBuilder();

String constantName = LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT.get(headerKey.toLowerCase(Locale.ROOT));
if ("CONTENT_TYPE".equals(constantName)) {
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add a comment to specify why we are skipping content type header handling here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants