Repository: javalin/javalin
Pull request #2394 (2025-04-17 06:24:26)

feat: add Context.disableCompression() to disable response compression
This PR adds `Context.disableCompression()` to allow disabling response compression on a per-endpoint basis.

Implements the feature discussed in [#2372](https://github.com/javalin/javalin/issues/2372).

Let me know if there’s anything I should refine before this can be merged.

2897robo: Hi, just wanted to kindly follow up on this PR 😊
Please let me know if there's anything I should improve before it can be reviewed or merged. Thanks!
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/javalin/javalin/pull/2394?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin) Report
Attention: Patch coverage is `0%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 0.00%. Comparing base [(`9fa8d83`)](https://app.codecov.io/gh/javalin/javalin/commit/9fa8d83c3183eb3d00629b1518e90b961ee8f743?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin) to head [(`1490b38`)](https://app.codecov.io/gh/javalin/javalin/commit/1490b388cde333406e3ba74d048c7d1de922575d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin).
> Report is 1 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/javalin/javalin/pull/2394?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin) | Patch % | Lines |
|---|---|---|
| [javalin/src/main/java/io/javalin/http/Context.kt](https://app.codecov.io/gh/javalin/javalin/pull/2394?src=pr&el=tree&filepath=javalin%2Fsrc%2Fmain%2Fjava%2Fio%2Fjavalin%2Fhttp%2FContext.kt&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin#diff-amF2YWxpbi9zcmMvbWFpbi9qYXZhL2lvL2phdmFsaW4vaHR0cC9Db250ZXh0Lmt0) | 0.00% | [1 Missing :warning: ](https://app.codecov.io/gh/javalin/javalin/pull/2394?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin) |

<details><summary>Additional details and impacted files</summary>


```diff
@@          Coverage Diff           @@
##           master   #2394   +/-   ##
======================================
  Coverage    0.00%   0.00%           
======================================
  Files         148     148           
  Lines        4498    4499    +1     
  Branches      513     513           
======================================
- Misses       4498    4499    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/javalin/javalin/pull/2394?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=javalin).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
2897robo: Hi @tipsy, thanks for the suggestion!

I added a test case for `Context.disableCompression()` that verifies compression is indeed disabled when a large response is returned. The test checks that the `Content-Encoding` header is not set even when compression would normally occur.

Let me know if there's anything else I should improve 🙌
2897robo: Hi @tipsy, thanks for the suggestion!

I added a test case for `Context.disableCompression()` that verifies compression is indeed disabled when a large response is returned. The test checks that the `Content-Encoding` header is not set even when compression would normally occur.

Let me know if there's anything else I should improve 🙌
2897robo: Thanks for the feedback @tipsy! 🙇

I’ve removed the Korean comments and pushed the changes.
Please let me know if there's anything else I should address before it can be reviewed or merged. Appreciate your time! 🙏
2897robo: Hi @tipsy, the Codecov check seems to be stuck. All other tests passed successfully.
Could you please review and merge if everything looks good? Thank you! 🙏

Patch:
diff --git a/javalin/src/main/java/io/javalin/http/Context.kt b/javalin/src/main/java/io/javalin/http/Context.kt
index 070fc962..50743695 100644
--- a/javalin/src/main/java/io/javalin/http/Context.kt
+++ b/javalin/src/main/java/io/javalin/http/Context.kt
@@ -359,6 +359,12 @@ interface Context {
      */
     fun minSizeForCompression(minSizeForCompression: Int): Context
 
+    /**
+     * Disables compression for the response output stream.
+     * Equivalent to calling [minSizeForCompression] with [Integer.MAX_VALUE].
+     */
+    fun disableCompression(): Context = minSizeForCompression(Int.MAX_VALUE)
+
     /**
      * Writes the specified inputStream as a seekable stream.
      * You can change this default in [io.javalin.config.JavalinConfig].


Test patch:
diff --git a/javalin/src/test/java/io/javalin/TestCompression.kt b/javalin/src/test/java/io/javalin/TestCompression.kt
index 7ef67ceb..d718c3b9 100644
--- a/javalin/src/test/java/io/javalin/TestCompression.kt
+++ b/javalin/src/test/java/io/javalin/TestCompression.kt
@@ -427,6 +427,18 @@ class TestCompression {
         }
     }
 
+    @Test
+    fun `disableCompression disables compression even if size is big`() = TestUtil.test(superCompressingApp()) { app, http ->
+        app.get("/disabled-compression") { ctx ->
+            ctx.disableCompression()
+            ctx.result("a".repeat(10_000))
+        }
+
+        val response = getResponse(http.origin, "/disabled-compression", "br, gzip")
+
+        assertThat(response.header(Header.CONTENT_ENCODING)).isNull()
+    }
+
     @Test
     fun `compresses a large Stream of JSON`() {
         data class Foo(val value: Long) // will become 19 chars in JSON, 20 with comma separator