Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

using cloner to make a deep clone of the cache before returning it #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package play.modules.swagger

import com.rits.cloning.Cloner
import io.swagger.config._
import io.swagger.models.Swagger
import play.api.Logger
Expand All @@ -8,6 +9,8 @@ object ApiListingCache {
var cache: Option[Swagger] = None

def listing(docRoot: String, host: String): Option[Swagger] = {
val cloner = new Cloner();

cache.orElse {
Logger("swagger").debug("Loading API metadata")

Expand All @@ -24,10 +27,14 @@ object ApiListingCache {
// no config, do nothing
}
}

cache = Some(swagger)
cache
val clone = cloner.deepClone(cache)
clone
}

cache.get.setHost(host)
cache
val clone = cloner.deepClone(cache)
clone
}
}
1 change: 1 addition & 0 deletions play-2.4/swagger-play2/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libraryDependencies ++= Seq(
"io.swagger" % "swagger-core" % "1.5.12",
"io.swagger" %% "swagger-scala-module" % "1.0.3",
"com.typesafe.play" %% "routes-compiler" % "2.4.6",
"uk.com.robust-it" % "cloning" % "1.9.2",
"com.typesafe.play" %% "play-ebean" % "2.0.0" % "test",
"org.specs2" %% "specs2-core" % "3.8.7" % "test",
"org.specs2" %% "specs2-mock" % "3.8.7" % "test",
Expand Down
16 changes: 15 additions & 1 deletion play-2.4/swagger-play2/test/PlayApiListingCacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ PUT /api/dog/:id testdata.DogController.add0(id:String)
ScannerFactory.setScanner(scanner)
val route = new RouteWrapper(routesRules)
RouteFactory.setRoute(route)

"ApiListingCache" should {

"load all API specs" in {
Expand Down Expand Up @@ -192,6 +192,20 @@ PUT /api/dog/:id testdata.DogController.add0(id:String)
dogDef.getProperties.containsKey("id") must beTrue
dogDef.getProperties.containsKey("name") must beTrue
}

"cache contains all paths after filtering" in {
val docRoot = ""
val swaggerAll = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerAll.get.getPaths.size must beEqualTo(7)

val swaggerCat = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerCat.get.setPaths(swaggerCat.get.getPaths.filterKeys(_.startsWith("/cat") ))
swaggerCat.get.getPaths.size must beEqualTo(2)

val swaggerDog = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerDog.get.setPaths(swaggerDog.get.getPaths.filterKeys(_.startsWith("/dog") ))
swaggerDog.get.getPaths.size must beEqualTo(2)
}
}

def toJsonString(data: Any): String = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package play.modules.swagger

import com.rits.cloning.Cloner
import io.swagger.config._
import io.swagger.models.Swagger
import play.api.Logger
Expand All @@ -8,6 +9,8 @@ object ApiListingCache {
var cache: Option[Swagger] = None

def listing(docRoot: String, host: String): Option[Swagger] = {
val cloner = new Cloner();

cache.orElse {
Logger("swagger").debug("Loading API metadata")

Expand All @@ -25,9 +28,11 @@ object ApiListingCache {
}
}
cache = Some(swagger)
cache
val clone = cloner.deepClone(cache)
clone
}
cache.get.setHost(host)
cache
val clone = cloner.deepClone(cache)
clone
}
}
1 change: 1 addition & 0 deletions play-2.5/swagger-play2/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ libraryDependencies ++= Seq(
"io.swagger" % "swagger-core" % "1.5.12",
"io.swagger" %% "swagger-scala-module" % "1.0.3",
"com.typesafe.play" %% "routes-compiler" % "2.5.4",
"uk.com.robust-it" % "cloning" % "1.9.2",
"com.typesafe.play" %% "play-ebean" % "2.0.0" % "test",
"org.specs2" %% "specs2-core" % "3.8.7" % "test",
"org.specs2" %% "specs2-mock" % "3.8.7" % "test",
Expand Down
14 changes: 14 additions & 0 deletions play-2.5/swagger-play2/test/PlayApiListingCacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ PUT /api/dog/:id testdata.DogController.add0(id:String)
dogDef.getProperties.containsKey("id") must beTrue
dogDef.getProperties.containsKey("name") must beTrue
}

"cache contains all paths after filtering" in {
val docRoot = ""
val swaggerAll = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerAll.get.getPaths.size must beEqualTo(7)

val swaggerCat = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerCat.get.setPaths(swaggerCat.get.getPaths.filterKeys(_.startsWith("/cat") ))
swaggerCat.get.getPaths.size must beEqualTo(2)

val swaggerDog = ApiListingCache.listing(docRoot, "127.0.0.1")
swaggerDog.get.setPaths(swaggerDog.get.getPaths.filterKeys(_.startsWith("/dog") ))
swaggerDog.get.getPaths.size must beEqualTo(2)
}
}

def toJsonString(data: Any): String = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package play.modules.swagger

import com.rits.cloning.Cloner
import io.swagger.config._
import io.swagger.models.Swagger
import play.api.Logger
Expand All @@ -8,6 +9,8 @@ object ApiListingCache {
var cache: Option[Swagger] = None

def listing(docRoot: String, host: String): Option[Swagger] = {
val cloner = new Cloner();

cache.orElse {
Logger("swagger").debug("Loading API metadata")

Expand All @@ -25,9 +28,11 @@ object ApiListingCache {
}
}
cache = Some(swagger)
cache
val clone = cloner.deepClone(cache)
clone
}
cache.get.setHost(host)
cache
val clone = cloner.deepClone(cache)
clone
}
}
1 change: 1 addition & 0 deletions play-2.6/swagger-play2/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ libraryDependencies ++= Seq(
"io.swagger" % "swagger-core" % "1.5.16",
"io.swagger" %% "swagger-scala-module" % "1.0.5-SNAPSHOT",
"com.typesafe.play" %% "routes-compiler" % "2.6.0",
"uk.com.robust-it" % "cloning" % "1.9.2",
"com.typesafe.play" %% "play-ebean" % "4.0.2" % "test",
"org.specs2" %% "specs2-core" % "3.8.7" % "test",
"org.specs2" %% "specs2-mock" % "3.8.7" % "test",
Expand Down