Skip to content

Commit

Permalink
airframe-http: #1128 Add Router.verifyRoutes to check duplicated endp…
Browse files Browse the repository at this point in the history
…oints (#1137)

* airframe-http: #1128 Add a helper method to find RPC method overload early

* Verify Router before generating HTTP client
  • Loading branch information
xerial committed Jun 18, 2020
1 parent fa3c458 commit c6768ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions airframe-http/.jvm/src/main/scala/wvlet/airframe/http/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ case class Router(
private lazy val routeMatcher = RouteMatcher.build(routes)
def findRoute[Req: HttpRequestAdapter](request: Req): Option[RouteMatch] = routeMatcher.findRoute(request)

/**
* Call this method to verify duplicated routes in an early phase
*/
def verifyRoutes: Unit = {
// Instantiate the route mappings to check duplicate routes
routeMatcher
}

/**
* Add methods annotated with @Endpoint to the routing table
*/
Expand Down Expand Up @@ -126,7 +134,7 @@ case class Router(
val endpointOpt = controllerSurface.findAnnotationOf[Endpoint]
val rpcOpt = controllerSurface.findAnnotationOf[RPC]

val newRoutes: Seq[Route] = {
val newRoutes: Seq[ControllerRoute] = {
(endpointOpt, rpcOpt) match {
case (Some(endpoint), Some(rpcOpt)) =>
throw new IllegalArgumentException(
Expand Down Expand Up @@ -158,7 +166,7 @@ case class Router(
} else {
s"${rpc.path()}/${serviceFullName}"
}
controllerMethodSurfaces
val routes = controllerMethodSurfaces
.filter(_.isPublic)
.map { m => (m, m.findAnnotationOf[RPC]) }
.collect {
Expand All @@ -168,6 +176,7 @@ case class Router(
case (m: ReflectMethodSurface, None) =>
ControllerRoute(rpcInterfaceCls, controllerSurface, HttpMethod.POST, prefixPath + s"/${m.name}", m)
}
routes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ object RouteScanner extends LogSupport {
router = router.addInternal(s, methods)
}
}
// Check whether the route is valid or not
router.verifyRoutes
router
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,18 @@ object RPCTest extends AirSpec {
m.get.method shouldBe HttpMethod.POST
m.get.methodSurface.name shouldBe "hello"
}

@RPC
trait RPCOverload {
def hello: String
def hello(s: String): String
}

test("Should detect RPC method overload") {
val r = Router.add[RPCOverload]
val e = intercept[IllegalArgumentException] {
r.verifyRoutes
}
e.getMessage.contains("RPCOverload/hello")
}
}

0 comments on commit c6768ed

Please sign in to comment.