Skip to content

Commit

Permalink
fix breaking change in MediaType.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Mar 4, 2024
1 parent 869b16c commit 9db38a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/src/main/scala/sttp/model/MediaType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ case class MediaType(

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

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

override def toString() = toStringCache
override lazy val hashCode: Int = toString.toLowerCase.hashCode

override def equals(that: Any): Boolean =
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/scala/sttp/model/MediaTypeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class MediaTypeTests extends AnyFlatSpec with Matchers with TableDrivenPropertyC
an[IllegalArgumentException] shouldBe thrownBy(MediaType.unsafeApply("te=xt", "plain"))
}

it should "have a regular toString" in {
val mt = MediaType.ApplicationGzip
mt.toString shouldBe "application/gzip"
mt.toString() shouldBe "application/gzip"
}

private val matchCases = Table(
("media type", "content type range", "matches"),
// simple matching
Expand Down

0 comments on commit 9db38a3

Please sign in to comment.