Skip to content

fix(http): support multi-valued headers and request isolation in HeadersInspectionHandler - #2214

Open
Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-multivalued-and-isolation
Open

Hardik K (hardikkaurani) wants to merge 2 commits into
microsoft:mainfrom
hardikkaurani:fix/headers-inspection-multivalued-and-isolation

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Summary

In HeadersInspectionHandler, there were three issues in header inspection and telemetry:

  1. Multi-valued Header Overwrites: HeadersInspectionHandler previously created a new HashSet<String>() per header and called put(header.getFirst(), value) on RequestHeaders and ResponseHeaders. When multiple entries existed for the same header name (e.g., Set-Cookie, Accept, Vary), subsequent values replaced the set instead of accumulating, leaving only the last value. By delegating to add(key, value), all values for the header are preserved.
  2. Request Isolation: If the same HeadersInspectionOption instance was reused across requests, headers from previous requests were never cleared and leaked into subsequent requests.
  3. Telemetry Attribute Accuracy: The OpenTelemetry span attribute com.microsoft.kiota.handler.headersInspection.enable was hardcoded to true even when inspection was disabled; it now reflects inspectionOption.getInspectRequestHeaders() || inspectionOption.getInspectResponseHeaders().

Changes

  • Replaced .put(header.getFirst(), value) with .add(header.getFirst(), header.getSecond()) in HeadersInspectionHandler.java.
  • Added .clear() calls to getRequestHeaders() and getResponseHeaders() prior to recording headers for each request/response.
  • Updated telemetry attribute to check if request or response inspection is enabled.
  • Added unit tests in HeadersInspectionHandlerTest.java verifying preservation of multi-valued headers and header isolation across requests.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 06:10
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner October 21, 2026 06:10

This comment was marked as off-topic.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet)! This PR fixes multi-valued header handling in HeadersInspectionHandler by calling add() instead of put() (which was replacing sets and overwriting multi-valued headers such as Set-Cookie or Vary), adds .clear() for per-request header isolation when reusing inspection options, and aligns the telemetry enable attribute with active inspection settings. Unit tests covering both multi-valued headers and request isolation are included. Whenever you have a chance, please take a look. Thank you!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Shared-option capture updates can race, and failed requests retain stale response headers.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 21, 2026 10:03
@hardikkaurani
Hardik K (hardikkaurani) force-pushed the fix/headers-inspection-multivalued-and-isolation branch from fde7d9f to d4bd310 Compare October 21, 2026 10:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hardikkaurani

Hardik K (hardikkaurani) commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet) and @andreastman,

I've addressed all Copilot review feedback and resolved the formatting and lint checks. Here's a summary of the changes:

1. Synchronize Shared Header Capture

  • Synchronized request and response header capture on the shared inspectionOption instance.
  • Prevented race conditions where concurrent requests without request-specific options could clear the internal header map between containsKey() and get() in Headers.addImpl(), avoiding potential NullPointerExceptions and cross-request header contamination.
  • Added handlesConcurrentCallsWithoutRaces() to verify race-free execution across concurrent requests using multiple threads.

2. Clear Response Headers Before chain.proceed(request)

  • Moved inspectionOption.getResponseHeaders().clear() before chain.proceed(request).
  • Ensured that if a request fails (e.g., with an IOException), stale response headers from a previous request using the shared option are not retained.
  • Added clearsResponseHeadersWhenRequestFails() to verify that a failed subsequent request clears stale response headers while preserving the request headers captured for that request.

3. Spotless Formatting and CI Checks

  • Applied formatting using ./gradlew spotlessApply.
  • Verified formatting using ./gradlew spotlessCheck.
  • Ran the relevant component checks and tests successfully.

Commits

The PR contains two focused commits:

  • 9dbe0f71: fix(http): support multi-valued headers, synchronize shared capture, and prevent stale response headers
  • d4bd310c: test(http): add unit tests for multi-valued headers, stale header prevention, and concurrent calls

All local checks and tests pass. The changes are ready for review. Thanks for your time and feedback!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The concurrency test must shut down its executor when workers fail or execution is interrupted.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment on lines +217 to +221
final List<Future<Void>> futures = executor.invokeAll(tasks);
for (final Future<Void> future : futures) {
future.get();
}
executor.shutdown();

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants