From 51b5bc58f2706c898caed44bce7ab21cb0f22fb0 Mon Sep 17 00:00:00 2001 From: Matthias Kurz Date: Mon, 8 Jul 2024 14:47:45 +0200 Subject: [PATCH] Can't override headers() in Scala 3 + stay compatible (cherry picked from commit 329a4bc596ab5f7e6228a319ffe3783597d7dd14) --- .../api/libs/ws/ahc/StreamedResponse.scala | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/play-ahc-ws-standalone/src/main/scala/play/api/libs/ws/ahc/StreamedResponse.scala b/play-ahc-ws-standalone/src/main/scala/play/api/libs/ws/ahc/StreamedResponse.scala index 76df5315..9872115c 100644 --- a/play-ahc-ws-standalone/src/main/scala/play/api/libs/ws/ahc/StreamedResponse.scala +++ b/play-ahc-ws-standalone/src/main/scala/play/api/libs/ws/ahc/StreamedResponse.scala @@ -42,23 +42,44 @@ class StreamedResponse( val status: Int, val statusText: String, val uri: java.net.URI, - headers: Map[String, scala.collection.Seq[String]], publisher: Publisher[HttpResponseBodyPart], val useLaxCookieEncoder: Boolean ) extends StandaloneWSResponse with CookieBuilder { + def this( + client: StandaloneAhcWSClient, + status: Int, + statusText: String, + uri: java.net.URI, + headers: Map[String, scala.collection.Seq[String]], + publisher: Publisher[HttpResponseBodyPart], + useLaxCookieEncoder: Boolean + ) = { + this( + client, + status, + statusText, + uri, + publisher, + useLaxCookieEncoder + ) + origHeaders = headers + } + + private var origHeaders: Map[String, scala.collection.Seq[String]] = Map.empty + /** * Get the underlying response object. */ override def underlying[T]: T = publisher.asInstanceOf[T] - override def headers(): Map[String, scala.collection.Seq[String]] = { + override lazy val headers: Map[String, scala.collection.Seq[String]] = { val mutableMap = mutable.TreeMap[String, scala.collection.Seq[String]]()(CaseInsensitiveOrdered) - headers.keys.foreach { name => + origHeaders.keys.foreach { name => mutableMap.updateWith(name) { - case Some(value) => Some(value ++ headers.getOrElse(name, Seq.empty)) - case None => Some(headers.getOrElse(name, Seq.empty)) + case Some(value) => Some(value ++ origHeaders.getOrElse(name, Seq.empty)) + case None => Some(origHeaders.getOrElse(name, Seq.empty)) } } TreeMap[String, scala.collection.Seq[String]]()(CaseInsensitiveOrdered) ++ mutableMap