Skip to content

Commit

Permalink
Allow docs for multiple routes with the same path
Browse files Browse the repository at this point in the history
  • Loading branch information
williamho committed Oct 5, 2015
1 parent bd5b022 commit c262c3a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/main/scala/com/iheart/playSwagger/SwaggerSpecGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ case class SwaggerSpecGenerator(domainNameSpace: Option[String] = None, defaultP
}


def endPointEntry(routeDocumentation: (String, String, String)): Option[(String, JsValue)] = {
def endPointEntry(routeDocumentation: (String, String, String)): Option[(String, JsObject)] = {
def methodDesc(raw: String) = raw.replace(" ", "").replace("@", "")
def methodPath(desc: String) = """(controllers[^\(]+)(\(.*\))?$""".r.findFirstMatchIn(desc).map(_.group(1))

Expand All @@ -206,15 +206,17 @@ case class SwaggerSpecGenerator(domainNameSpace: Option[String] = None, defaultP
else if(s"${marker}\\s*NoDocs\\s*${marker}".r.findFirstIn(commentLines.mkString("\n")).isDefined)
None
else {
val path = rawPath.replaceAll( """\$(\w+)<[^>]+>""", "{$1}")
val path = rawPath.replaceAll("""\$(\w+)<[^>]+>""", "{$1}")
Some(path Json.obj(method.toLowerCase -> endPointSpec(controllerDesc, commentLines, path)))
}
}

JsObject(routesDocumentation.map(endPointEntry).collect { case Some(o) o })
// Multiple routes may have the same path, merge the objects instead of overwriting
JsObject {
routesDocumentation.flatMap(endPointEntry)
.groupBy(_._1) // Routes grouped by path
.mapValues(_.map(_._2).reduce(_ deepMerge _))
}
}
}




Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ class SwaggerSpecGeneratorSpec extends Specification {
"integration" >> {
val routesDocumentation = Seq(
("GET","/api/artist/$aid<[^/]+>/playedTracks/recent","controllers.LiveMeta.playedByArtist(aid:Int, limit:Option[Int])"),

("GET","/api/station/$sid<[^/]+>/playedTracks/last", "@controllers.LiveMeta@.playedByStation(sid:Int)"),
("POST","/api/station/playedTracks", "controllers.LiveMeta.addPlayedTracks()"),
("GET","/api/station/hidden", "controllers.LiveMeta.hiddenEndPoint()"),

("GET","/api/player/$pid<.+>/context/$bid<.+>", "controllers.Player.getPlayer(pid:String, bid:String)"),
("GET","/api/player/$pid<.+>/tracks/search", "controllers.Player.searchTrack(pid:String, bid:String)"),
("POST","/api/player/$pid<.+>/playedTracks", "controllers.Player.addPlayedTracks(pid:String)"),
("GET","/api/station/hidden", "controllers.LiveMeta.hiddenEndPoint()")

("GET", "/api/resource/", "controllers.Resource.get()"),
("PUT", "/api/resource/", "controllers.Resource.put()"),
("POST", "/api/resource/", "controllers.Resource.post()"),
("DELETE", "/api/resource/", "controllers.Resource.post()")
)
val liveMetaRoutesLines =
"""
Expand Down Expand Up @@ -81,6 +88,14 @@ class SwaggerSpecGeneratorSpec extends Specification {
|
""".stripMargin.split("\n").toList

val resourceRoutesLines =
"""
|GET /api/resource/ controllers.Resource.get()
|PUT /api/resource/ controllers.Resource.put()
|POST /api/resource/ controllers.Resource.post()
|DELETE /api/resource/ controllers.Resource.delete()
""".stripMargin.split("\n").toList

val base = Json.parse(
"""
|{
Expand All @@ -93,7 +108,10 @@ class SwaggerSpecGeneratorSpec extends Specification {
|}
""".stripMargin).asInstanceOf[JsObject]

val routesLines = Map("liveMeta" liveMetaRoutesLines, "player" playerRoutesLines)
val routesLines = Map(
"liveMeta" liveMetaRoutesLines,
"player" playerRoutesLines,
"resource" resourceRoutesLines)


val json = SwaggerSpecGenerator(Some("com.iheart")).generateWithBase(routesDocumentation, routesLines, base)
Expand All @@ -104,6 +122,7 @@ class SwaggerSpecGeneratorSpec extends Specification {
val addTrackJson = (pathJson \ "/api/station/playedTracks" \ "post").as[JsObject]
val playerJson = (pathJson \ "/api/player/{pid}/context/{bid}" \ "get").as[JsObject]
val playerAddTrackJson = (pathJson \ "/api/player/{pid}/playedTracks" \ "post").as[JsObject]
val resourceJson = (pathJson \ "/api/resource/").as[JsObject]
val artistDefJson = (definitionsJson \ "com.iheart.playSwagger.Artist").as[JsObject]


Expand Down Expand Up @@ -182,7 +201,7 @@ class SwaggerSpecGeneratorSpec extends Specification {
"generate tags definition" >> {
val tags = (json \ "tags").asOpt[Seq[JsObject]]
tags must beSome[Seq[JsObject]]
tags.get.map( tO (tO \ "name").as[String]).sorted === Seq("liveMeta", "player").sorted
tags.get.map(tO (tO \ "name").as[String]).sorted === Seq("liveMeta", "player", "resource").sorted
}

"merge tag description from base" >> {
Expand All @@ -199,7 +218,6 @@ class SwaggerSpecGeneratorSpec extends Specification {

}


"get parameter type of" >> {
val playerSearchJson = (pathJson \ "/api/player/{pid}/tracks/search" \ "get").as[JsObject]
val params: Seq[JsValue] = parametersOf(playerSearchJson)
Expand All @@ -216,6 +234,11 @@ class SwaggerSpecGeneratorSpec extends Specification {

}

"allow multiple routes with the same path" >> {
resourceJson.keys.toSet === Set("get", "post", "delete", "put")
}

}

}

0 comments on commit c262c3a

Please sign in to comment.