Add cache headers to document URLs endpoint#3766
Conversation
Co-authored-by: martincostello <1439341+martincostello@users.noreply.github.com>
Co-authored-by: martincostello <1439341+martincostello@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR ensures the Swagger UI document URLs endpoint (/swagger/documentUrls) participates in the same cache-busting behavior as other Swagger UI resources by adding ETag and cache headers. It also refactors ETag computation within SwaggerUIMiddleware to avoid duplication and adds an integration test around the new behavior.
Changes:
- Updated
SwaggerUIMiddleware.RespondWithDocumentUrlsto compute a SHA1-based ETag for the JSON payload and apply cache headers via the existingSetHeadershelper. - Refactored the ETag helper from a local static function in
RespondWithFileinto a shared private staticHashTextmethod onSwaggerUIMiddleware. - Added an integration test
DocumentUrlsEndpoint_ReturnsJsonWithCacheHeadersto verify that/swagger/documentUrlsreturns the expected content type, body contents, weak ETag, andCache-Controlheaders (private with 7-day max-age under default configuration).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIMiddleware.cs | Reuses a shared HashText helper and updates RespondWithDocumentUrls to emit weak ETags and cache headers consistent with other Swagger UI responses. |
| test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerUIIntegrationTests.cs | Adds an integration test for /swagger/documentUrls to assert content, weak ETag presence, and Cache-Control configuration under the default MultipleVersions test site setup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerUIIntegrationTests.cs
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3766 +/- ##
==========================================
+ Coverage 94.67% 94.90% +0.23%
==========================================
Files 111 111
Lines 3868 3870 +2
Branches 780 780
==========================================
+ Hits 3662 3673 +11
+ Misses 206 197 -9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The issue or feature being addressed
The document URLs endpoint (
/swagger/documentUrls) lacks cache control headers, causing browsers to cache stale data indefinitely.Details on the issue fix or feature implementation
Updated
RespondWithDocumentUrlsto compute SHA1-based ETag and apply cache headers via existingSetHeadersmethod, matching the pattern inRespondWithFile:Extracted
HashTextas a shared static method to eliminate duplication betweenRespondWithFileandRespondWithDocumentUrls.Added integration test
DocumentUrlsEndpoint_ReturnsJsonWithCacheHeadersverifying ETag (weak) and Cache-Control headers (Private, MaxAge=7d or no-cache/no-store based onCacheLifetimeconfig).Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
scarf.sh/usr/local/bin/node node ./report.js(dns block)/usr/local/bin/node node ./report.js artifacts/bin/Cl--noprofile(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.