Skip to content

Stop output/response caching from reordering request values - #69112

Merged
Youssef1313 merged 3 commits into
mainfrom
copilot/fix-output-cache-key-provider
Sep 8, 2026
Merged

Youssef1313 merged 3 commits into
mainfrom
copilot/fix-output-cache-key-provider

Conversation

Copilot AI commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Avoids sorting headers and query parameters when calculating the cache key.

Fixes #69109

Copilot AI changed the title [WIP] Fix OutputCacheKeyProvider mutating request query and header values Stop OutputCacheKeyProvider from reordering request values Sep 8, 2026
Copilot AI requested a review from Youssef1313 September 8, 2026 07:55
Copilot AI requested a review from Youssef1313 September 8, 2026 08:12
@Youssef1313
Youssef1313 force-pushed the copilot/fix-output-cache-key-provider branch from 2c4e4a9 to 2d0b6c1 Compare September 8, 2026 08:14
@Youssef1313 Youssef1313 added Servicing-consider Shiproom approval is required for the issue and removed Servicing-consider Shiproom approval is required for the issue labels Sep 8, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hi @copilot. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

@Youssef1313 Youssef1313 changed the title Stop OutputCacheKeyProvider from reordering request values Stop output/response caching from reordering request values Sep 8, 2026
@Youssef1313

Copy link
Copy Markdown
Member

@copilot Fix response caching as well

Copilot AI commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Fix response caching as well

Done in commit 5789ba8. ResponseCachingKeyProvider now avoids sorting selected-header and query values, and I added response-caching regression coverage for explicit query, wildcard query, and selected-header non-mutation.

@Youssef1313
Youssef1313 marked this pull request as ready for review September 8, 2026 08:23
Copilot AI lite review requested due to automatic review settings September 8, 2026 08:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Key-generation behavior changes (order-sensitivity vs canonicalization) need resolution/clarification, and there are test naming consistency fixes requested.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates the OutputCaching and ResponseCaching key providers to avoid mutating live HttpRequest.Headers / HttpRequest.Query value ordering during cache-key generation, and adjusts/adds tests to cover the new behavior.

Changes:

  • Removed in-place sorting of repeated header/query values during vary-by key generation in OutputCaching and ResponseCaching.
  • Updated existing key-generation test expectations to reflect preserved repeated-value order.
  • Added targeted regression tests asserting request header/query value order is unchanged after key generation.
File summaries
File Description
src/Middleware/ResponseCaching/test/ResponseCachingKeyProviderTests.cs Updates expected vary-by keys and adds tests to ensure request value order isn’t mutated.
src/Middleware/ResponseCaching/src/ResponseCachingKeyProvider.cs Removes sorting of repeated header/query values when building the vary-by storage key.
src/Middleware/OutputCaching/test/OutputCacheKeyProviderTests.cs Updates expected cache keys and adds tests to ensure request value order isn’t mutated.
src/Middleware/OutputCaching/src/OutputCacheKeyProvider.cs Removes sorting of repeated header/query values when appending vary-by segments to the storage key.
Review details

Suppressed comments (2)

src/Middleware/ResponseCaching/test/ResponseCachingKeyProviderTests.cs:246

  • Test method prefix uses "CreateStorageVaryByKey" while the rest of this file uses "CreateStorageVaryKey"; for consistency within the suite, consider renaming this method to match the established pattern.
    public void ResponseCachingKeyProvider_CreateStorageVaryByKey_ExplicitQueryValues_DoesNotMutateRequestQueryValueOrder()

src/Middleware/ResponseCaching/test/ResponseCachingKeyProviderTests.cs:262

  • Test method prefix uses "CreateStorageVaryByKey" while the rest of this file uses "CreateStorageVaryKey"; for consistency within the suite, consider renaming this method to match the established pattern.
    public void ResponseCachingKeyProvider_CreateStorageVaryByKey_WildcardQueryValues_DoesNotMutateRequestQueryValueOrder()
  • Files reviewed: 4/4 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@halter73 halter73 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Initially, I was worried we were also dropping query parameter name sorting, which could cause unnecessary cache misses.

But on closer inspection, I realize ?a=1&b=2 still matches ?b=2&a=1 after this PR, and only the value order within each name becomes significant. Given our APIs expose those values as ordered StringValues, it’s easy for application code to depend on that order for both query parameters and headers. Preserving it in the cache key makes sense. LGTM.

@Youssef1313
Youssef1313 merged commit 67a02a6 into main Sep 8, 2026
27 of 28 checks passed
@Youssef1313
Youssef1313 deleted the copilot/fix-output-cache-key-provider branch September 8, 2026 21:14
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OutputCacheKeyProvider mutates request query and header values

4 participants