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

Fix for routes with different parameter names #2903

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions zio-http/jvm/src/test/scala/zio/http/RoutesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,29 @@ object RoutesSpec extends ZIOHttpSpec {
result <- app.runZIO(Request(body = body))
} yield assertTrue(result.body == body)
},
test("routes with different path parameter arities should all be handled") {
val one = Method.GET / string("first") -> Handler.ok
val getone = Request.get("/1")

val two = Method.GET / string("prefix") / string("second") -> Handler.internalServerError
val gettwo = Request.get("/2/two")

val onetwo = Routes(one, two)
val twoone = Routes(two, one)

for {
onetwoone <- onetwo.runZIO(getone)
onetwotwo <- onetwo.runZIO(gettwo)
twooneone <- twoone.runZIO(getone)
twoonetwo <- twoone.runZIO(gettwo)
} yield {
assertTrue(
extractStatus(onetwoone) == Status.Ok,
extractStatus(onetwotwo) == Status.InternalServerError,
extractStatus(twooneone) == Status.Ok,
extractStatus(twoonetwo) == Status.InternalServerError,
)
}
},
)
}
63 changes: 48 additions & 15 deletions zio-http/shared/src/main/scala/zio/http/codec/PathCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -501,24 +501,57 @@ object PathCodec {
result = subtree.value
i = i + 1
} else {
// Slower fallback path. Have to evaluate all predicates at this node:
val flattened = subtree.othersFlat

var index = 0
subtree = null

while ((index < flattened.length) && (subtree eq null)) {
val tuple = flattened(index)
val matched = tuple._1.matches(segments, i)

if (matched >= 0) {
subtree = tuple._2
result = subtree.value
i = i + matched
} else {
// No match found. Keep looking at alternate routes:
index += 1
}
flattened.length match {
case 0 => // No predicates to evaluate
case 1 => // Only 1 predicate to evaluate (most common)
val (codec, subtree0) = flattened(0)
val matched = codec.matches(segments, i)
if (matched > 0) {
subtree = subtree0
result = subtree0.value
i = i + matched
}
case n => // Slowest fallback path. Have to to find the first predicate where the subpath returns a result
val matches = Array.ofDim[Int](n)
var index = 0
var nPositive = 0
var lastPositiveIdx = -1
while (index < n) {
val (codec, _) = flattened(index)
val n = codec.matches(segments, i)
if (n > 0) {
matches(index) = n
nPositive += 1
lastPositiveIdx = index
}
index += 1
}

nPositive match {
case 0 => ()
case 1 =>
subtree = flattened(lastPositiveIdx)._2
result = subtree.value
i = i + matches(lastPositiveIdx)
case _ =>
index = 0
while (index < n && (subtree eq null)) {
val matched = matches(index)
if (matched > 0) {
val (_, subtree0) = flattened(index)
val subpath = path.dropLeadingSlash.drop(i + matched)
Copy link
Member

Choose a reason for hiding this comment

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

Any way to eliminate these allocations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we can overload the get method to accept an index and use that as the starting i in this method. I'll open a separate PR

if (subtree0.get(subpath).nonEmpty) {
subtree = subtree0
result = subtree.value
i += matched
}
}
index += 1
}
}
}

if (subtree eq null) {
Expand Down
Loading