Make HttpHeaders strings const
Subsystem ktor-http
Motivation Primarily to help make these strings usable in compile-time contexts, e.g. as annotation parameters. The MayBeConstant suppression makes me think there might be a reason for them not being const already, but I couldn't find any reasoning for that besides ABI compatibility.
Solution Make immutable strings const.
Walkthrough
Header-name declarations in io.ktor.http.HttpHeaders were converted from runtime vals/getters to compile-time const val constants; many deprecated getter-style accessors and underscored aliases were added for compatibility; UnsafeHeaders and UnsafeHeadersList were introduced and API/klib metadata updated.
Changes
| Cohort / File(s) | Summary |
|---|---|
Const migration (source + APIs)ktor-http/common/src/io/ktor/http/HttpHeaders.kt, ktor-http/api/ktor-http.api, ktor-http/api/ktor-http.klib.api |
Many HTTP header declarations moved from runtime val/getter forms to const val compile-time constants (e.g., ALPN, Accept, AcceptCharset, …, XTotalCount). API and klib metadata updated to reflect const qualifiers. |
Deprecated getter aliases & underscored propertiesktor-http/common/src/io/ktor/http/HttpHeaders.kt |
Added deprecated getter-style accessors and underscored alias properties with @get:JvmName and @Deprecated(..., ReplaceWith(...)) returning the new constants to preserve existing usages. |
Unsafe headers additionsktor-http/common/src/io/ktor/http/HttpHeaders.kt, ktor-http/api/ktor-http.klib.api |
Introduced UnsafeHeaders: Array<String> and UnsafeHeadersList: List<String> as public members with accessors; exported in klib API metadata. |
API cleanup — removed JVM gettersktor-http/api/ktor-http.api |
Removed many per-header getter functions (e.g., getAccept(), getContentType(), …) in favor of public string fields/constants (reflected in the JVM API file). |
Annotations / suppressionsktor-http/common/src/io/ktor/http/HttpHeaders.kt |
Updated @Suppress to include ConstPropertyName and adjusted JVM name annotations and deprecation metadata formatting. |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
- Review focus:
- Verify deprecated alias getters preserve JVM getter names/signatures exactly.
- Confirm API/klib metadata matches source changes (const vs val, removed getters).
- Check
ReplaceWithexpressions and@get:JvmNameannotations for accuracy. - Inspect initialization, visibility, and contents of
UnsafeHeaders/UnsafeHeadersList.
Suggested reviewers
- bjhham
Pre-merge checks and finishing touches
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 1.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description check | ✅ Passed | The description follows the template structure with all required sections (Subsystem, Motivation, Solution) completed. It clearly explains the problem, rationale, and solution. |
| Title check | ✅ Passed | The title clearly and concisely summarizes the main change: converting HttpHeaders string values from mutable vals to compile-time constants (const). |
✨ Finishing touches
- [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
but I couldn't find any reasoning for that besides ABI compatibility.
This is actually quite important... Perhaps there is a way to preserve compatibility via Jvm annotations or something?
YouTrack issue: KTOR-9199 Make HttpHeaders strings const