Skip to content

Commit 9db38a3

Browse files
committed
fix breaking change in MediaType.toString
1 parent 869b16c commit 9db38a3

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

core/src/main/scala/sttp/model/MediaType.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ case class MediaType(
6363

6464
def isModel: Boolean = mainType.equalsIgnoreCase("model")
6565

66-
override lazy val toString: String = {
66+
// Cache 'toString' given that it's called in the hot path
67+
// of request processing to generate headers.
68+
private lazy val toStringCache: String = {
6769
val sb = new java.lang.StringBuilder(32) // "application/json; charset=utf-8".length == 31 ;)
6870
sb.append(mainType).append('/').append(subType)
6971
charset match {
@@ -77,6 +79,7 @@ case class MediaType(
7779
sb.toString
7880
}
7981

82+
override def toString() = toStringCache
8083
override lazy val hashCode: Int = toString.toLowerCase.hashCode
8184

8285
override def equals(that: Any): Boolean =

core/src/test/scala/sttp/model/MediaTypeTests.scala

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class MediaTypeTests extends AnyFlatSpec with Matchers with TableDrivenPropertyC
5959
an[IllegalArgumentException] shouldBe thrownBy(MediaType.unsafeApply("te=xt", "plain"))
6060
}
6161

62+
it should "have a regular toString" in {
63+
val mt = MediaType.ApplicationGzip
64+
mt.toString shouldBe "application/gzip"
65+
mt.toString() shouldBe "application/gzip"
66+
}
67+
6268
private val matchCases = Table(
6369
("media type", "content type range", "matches"),
6470
// simple matching

0 commit comments

Comments
 (0)