Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve showShort to avoid parsing Uri #3590

Merged
merged 8 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/main/scala/sttp/tapir/model/ServerRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import scala.collection.immutable.Seq
trait ServerRequest extends RequestMetadata {
def protocol: String
def connectionInfo: ConnectionInfo
/** Override in backend-specific implementation if you want a more efficient implementation and avoid uri parsing overhead */
def uriStr: String = uri.copy(scheme = None, authority = None, fragmentSegment = None).toString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe impls should simple override showShort? Since we are doing some customisations here (removing the scheme etc.), which are not necessarily general

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, we should rename uriStr to showUriShort

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, since uriStr will be used only rather only for showShort it seems like a good idea to skip it and just override that method 👍

def underlying: Any

/** Can differ from `uri.path`, if the endpoint is deployed in a context */
Expand Down Expand Up @@ -50,8 +52,9 @@ trait ServerRequest extends RequestMetadata {
this
)


/** A short representation of this request, including the request method, path and query. */
def showShort: String = s"$method ${uri.copy(scheme = None, authority = None, fragmentSegment = None).toString}"
def showShort: String = s"$method $uriStr"
}

class ServerRequestOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private[akkahttp] case class AkkaServerRequest(ctx: RequestContext, attributes:
run(query, Nil)
}

override lazy val uriStr: String = s"${ctx.request.uri.path}${ctx.request.uri.rawQueryString.getOrElse("")}"
override lazy val uri: Uri = {
val pekkoUri = ctx.request.uri
Uri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ private[http4s] case class Http4sServerRequest[F[_]](req: Request[F], attributes
override lazy val queryParameters: QueryParams = QueryParams.fromMultiMap(req.multiParams)

override def method: Method = Method(req.method.name.toUpperCase)
override lazy val uriStr: String = req.uri.copy(scheme = None, authority = None, fragment = None).toString
override lazy val uri: Uri =
Uri.apply(
req.uri.scheme.map(_.value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ case class NettyServerRequest(req: HttpRequest, attributes: AttributeMap = Attri
QueryParams.fromMultiMap(multiMap)
}
override lazy val method: Method = Method.unsafeApply(req.method().name())
override lazy val uriStr: String = req.uri()
override lazy val uri: Uri = Uri.unsafeParse(req.uri())
override lazy val pathSegments: List[String] = uri.pathSegments.segments.map(_.v).filter(_.nonEmpty).toList
override lazy val headers: Seq[Header] = req.headers().toHeaderSeq ::: (req match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private[pekkohttp] case class PekkoServerRequest(ctx: RequestContext, attributes
run(query, Nil)
}

override lazy val uriStr: String = s"${ctx.request.uri.path}${ctx.request.uri.rawQueryString.getOrElse("")}"
override lazy val uri: Uri = {
val pekkoUri = ctx.request.uri
Uri(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ private[vertx] case class VertxServerRequest(rc: RoutingContext, attributes: Att
}
override lazy val method: Method = MethodMapping.vertxToSttp(rc.request)
override lazy val protocol: String = Option(rc.request.scheme).getOrElse("")
override lazy val uriStr: String = rc.request.uri
override lazy val uri: Uri = Uri.unsafeParse(rc.request.uri)
override lazy val headers: Seq[Header] = rc.request.headers.entries.asScala.iterator.map(e => Header(e.getKey, e.getValue)).toList
override lazy val queryParameters: QueryParams = Uri.unsafeParse(rc.request.uri()).params
Expand Down
Loading