Skip to content

Commit

Permalink
GH-51 Improve default mappings of interceptors (Fix #51)
Browse files Browse the repository at this point in the history
* fix: improve default mappings of interceptors

* Update routing-annotations/routing-annotated/src/test/java/io/javalin/community/routing/annotations/AnnotatedRoutingTest.kt

Co-authored-by: Itzdlg <[email protected]>

---------

Co-authored-by: Itzdlg <[email protected]>
  • Loading branch information
dzikoysk and Itzdlg authored Jan 16, 2025
1 parent eaf6490 commit e6f40cb
Show file tree
Hide file tree
Showing 3 changed files with 447 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,32 @@ internal class ReflectiveEndpointLoader(
val status = method.getAnnotation<Status>()
val resultHandler = findResultHandler(method)

val route = AnnotatedRoute(
method = httpMethod,
path = ("/$endpointPath/$path").replace(repeatedPathSeparatorRegex, "/"),
version = method.getAnnotation<Version>()?.value,
handler = {
val arguments = argumentSuppliers
.map { it(this, Unit) }
.toTypedArray()
val declaredPath = "/$endpointPath/$path".replace(repeatedPathSeparatorRegex, "/").dropLastWhile { it == '/' }
val processedPaths = mutableSetOf(declaredPath)

when (async) {
true -> async { invokeAndUnwrapIfErrored(method, endpoint, arguments, ctx = this, status = status, resultHandler = resultHandler) }
else -> invokeAndUnwrapIfErrored(method, endpoint, arguments, ctx = this, status = status, resultHandler = resultHandler)
}
}
)
if (!httpMethod.isHttpMethod && declaredPath.endsWith("*")) {
processedPaths.add(declaredPath.dropLast(1).dropLastWhile { it == '/' })
}

endpointRoutes.add(route)
processedPaths.forEach { pathToAdd ->
endpointRoutes.add(
AnnotatedRoute(
method = httpMethod,
path = pathToAdd,
version = method.getAnnotation<Version>()?.value,
handler = {
val arguments = argumentSuppliers
.map { it(this, Unit) }
.toTypedArray()

when (async) {
true -> async { invokeAndUnwrapIfErrored(method, endpoint, arguments, ctx = this, status = status, resultHandler = resultHandler) }
else -> invokeAndUnwrapIfErrored(method, endpoint, arguments, ctx = this, status = status, resultHandler = resultHandler)
}
}
)
)
}
}

return endpointRoutes
Expand Down
Loading

0 comments on commit e6f40cb

Please sign in to comment.