diff --git a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/Uri.scala b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/Uri.scala index d93e6860d0..72e4e7f9d0 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/Uri.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/Uri.scala @@ -129,7 +129,8 @@ sealed abstract case class Uri(scheme: String, authority: Authority, path: Path, /** * Returns a copy of this Uri with the given query. */ - def withQuery(query: Query): Uri = copy(rawQueryString = if (query.isEmpty) None else Some(query.toString)) + def withQuery(query: Query): Uri = + createUnsafe(scheme, authority, path, if (query.isEmpty) None else Some(query.toString), fragment) /** * Returns a copy of this Uri with the given query string. diff --git a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/UriSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/UriSpec.scala index 557f837aba..b98965d30f 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/UriSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/scaladsl/model/UriSpec.scala @@ -818,6 +818,12 @@ class UriSpec extends AnyWordSpec with Matchers { uri.withRawQueryString("param1=val%22ue1") shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") uri.withRawQueryString("param1=val\"ue1") shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") + uri.withQuery(Query("param1=val\"ue1")) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") + uri.withQuery(Query(("param1", "val\"ue1"))) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") + uri.withQuery(Query(Some("param1=val\"ue1"))) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") + val query = Query.newBuilder.+=("param1" -> "val\"ue1").result() + uri.withQuery(query) shouldEqual Uri("http://host/path?param1=val%22ue1#fragment") + uri.withFragment("otherFragment") shouldEqual Uri("http://host/path?query#otherFragment") }