azure-sdk-for-java icon indicating copy to clipboard operation
azure-sdk-for-java copied to clipboard

Fix setAll method transformation in HttpHeadersCustomRecipe to properly convert Map to HttpHeaders

Open Copilot opened this issue 8 months ago • 2 comments

The HttpHeadersCustomRecipe contained a logic bug in the setAll method migration that incorrectly assumed type compatibility between java.util.Map and io.clientcore.core.http.models.HttpHeaders.

Problem

The original transformation was:

// Before (azure-core)
headers.setAll(someMap);  // Map<String, List<String>>

// After (buggy clientcore transformation)  
headers.setAll(someMap);  // Treated someMap as HttpHeaders - incorrect!

This failed because:

  • Azure-core HttpHeaders.setAll() accepted Map<String, List<String>>
  • Clientcore HttpHeaders.setAll() only accepts HttpHeaders objects
  • The template assumed direct type compatibility without conversion

Solution

Now properly converts the Map to HttpHeaders using stream-based collection:

// Before (azure-core)
headers.setAll(headerMap);

// After (fixed clientcore transformation)
headers.setAll(headerMap.entrySet().stream().collect(
    HttpHeaders::new, 
    (newHeaders, entry) -> newHeaders.set(
        HttpHeaderName.fromString(entry.getKey()), 
        entry.getValue() instanceof java.util.List ? 
            (java.util.List<String>) entry.getValue() : 
            java.util.Collections.singletonList(entry.getValue().toString())
    ), 
    HttpHeaders::setAll
));

Key Features

  • ✅ Creates new HttpHeaders instance from the input Map
  • ✅ Handles both String and List<String> values properly
  • ✅ Uses HttpHeaderName.fromString() for proper key conversion
  • ✅ No variable assignments in template (customer code agnostic)
  • ✅ Adds all necessary imports (HttpHeaders, HttpHeaderName, Collections)

Testing

Added comprehensive test case demonstrating the transformation with realistic Map usage including multiple header types.

Fixes #45608.


💡 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.

Copilot avatar Jun 05 '25 17:06 Copilot

@copilot The source code minimum is java 8. We cannot use text blocks. Alter the implementation to be Java 8 compatible.

jairmyree avatar Jun 10 '25 17:06 jairmyree

@copilot The source code minimum is java 8. We cannot use text blocks. Alter the implementation to be Java 8 compatible.

Fixed in commit 3ab7350. Converted text blocks to Java 8 compatible string concatenation using + operator and explicit newline characters.

Copilot avatar Jun 10 '25 17:06 Copilot

API Change Check

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

com.azure:azure-openrewrite

github-actions[bot] avatar Jun 24 '25 16:06 github-actions[bot]

Hi @Copilot. Thank you for your interest in helping to improve the Azure SDK experience and for your contribution. We've noticed that there hasn't been recent engagement on this pull request. If this is still an active work stream, please let us know by pushing some changes or leaving a comment. Otherwise, we'll close this out in 7 days.

github-actions[bot] avatar Oct 17 '25 05:10 github-actions[bot]

Hi @Copilot. Thank you for your interest in helping to improve the Azure SDK experience and for your contribution. We've noticed that there hasn't been recent engagement on this pull request. If this is still an active work stream, please let us know by pushing some changes or leaving a comment. Otherwise, we'll close this out in 7 days.

github-actions[bot] avatar Dec 19 '25 05:12 github-actions[bot]