From d46041a2ad4c83a65f565211343bbacddef5fb9d Mon Sep 17 00:00:00 2001 From: Bernhard Altendorfer Date: Thu, 14 Jul 2016 08:46:49 +0200 Subject: [PATCH 1/4] disabling the listing cache --- .../play/modules/swagger/ApiListingCache.scala | 16 ++++++++++------ .../test/PlayApiListingCacheSpec.scala | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala b/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala index a270cc4..da9c985 100644 --- a/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala +++ b/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala @@ -8,7 +8,7 @@ object ApiListingCache { var cache: Option[Swagger] = None def listing(docRoot: String, host: String): Option[Swagger] = { - cache.orElse { + //cache.orElse { Logger("swagger").debug("Loading API metadata") val scanner = ScannerFactory.getScanner() @@ -24,10 +24,14 @@ object ApiListingCache { // no config, do nothing } } - cache = Some(swagger) - cache - } - cache.get.setHost(host) - cache + + Some(swagger) + + //cache = Some(swagger) + //cache + //} + + //cache.get.setHost(host) + //cache } } diff --git a/play-2.4/swagger-play2/test/PlayApiListingCacheSpec.scala b/play-2.4/swagger-play2/test/PlayApiListingCacheSpec.scala index f2be446..2d670d7 100644 --- a/play-2.4/swagger-play2/test/PlayApiListingCacheSpec.scala +++ b/play-2.4/swagger-play2/test/PlayApiListingCacheSpec.scala @@ -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 { @@ -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 = { From d676a7f3968c874a40032e813a3f7e96e30d4fa4 Mon Sep 17 00:00:00 2001 From: Bernhard Altendorfer Date: Mon, 18 Jul 2016 09:02:41 +0200 Subject: [PATCH 2/4] using cloner to make a deep clone of the cache before returning it --- .../modules/swagger/ApiListingCache.scala | 19 +++++++++++-------- play-2.4/swagger-play2/build.sbt | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala b/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala index da9c985..3f526e7 100644 --- a/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala +++ b/play-2.4/swagger-play2/app/play/modules/swagger/ApiListingCache.scala @@ -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 @@ -8,7 +9,9 @@ object ApiListingCache { var cache: Option[Swagger] = None def listing(docRoot: String, host: String): Option[Swagger] = { - //cache.orElse { + val cloner = new Cloner(); + + cache.orElse { Logger("swagger").debug("Loading API metadata") val scanner = ScannerFactory.getScanner() @@ -25,13 +28,13 @@ object ApiListingCache { } } - Some(swagger) - - //cache = Some(swagger) - //cache - //} + cache = Some(swagger) + val clone = cloner.deepClone(cache) + clone + } - //cache.get.setHost(host) - //cache + cache.get.setHost(host) + val clone = cloner.deepClone(cache) + clone } } diff --git a/play-2.4/swagger-play2/build.sbt b/play-2.4/swagger-play2/build.sbt index 3de28b5..c919e6f 100644 --- a/play-2.4/swagger-play2/build.sbt +++ b/play-2.4/swagger-play2/build.sbt @@ -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", From f56e127b28f6ef1495047c4f7e7b16f711d03a57 Mon Sep 17 00:00:00 2001 From: Bernhard Altendorfer Date: Tue, 18 Oct 2016 15:40:06 +0200 Subject: [PATCH 3/4] using cloner to make a deep clone of the cache before returning it in version 2.5 --- .../app/play/modules/swagger/ApiListingCache.scala | 9 +++++++-- play-2.5/swagger-play2/build.sbt | 1 + .../test/PlayApiListingCacheSpec.scala | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/play-2.5/swagger-play2/app/play/modules/swagger/ApiListingCache.scala b/play-2.5/swagger-play2/app/play/modules/swagger/ApiListingCache.scala index a270cc4..40e5ce3 100644 --- a/play-2.5/swagger-play2/app/play/modules/swagger/ApiListingCache.scala +++ b/play-2.5/swagger-play2/app/play/modules/swagger/ApiListingCache.scala @@ -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 @@ -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") @@ -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 } } diff --git a/play-2.5/swagger-play2/build.sbt b/play-2.5/swagger-play2/build.sbt index cd447cd..8c535f0 100644 --- a/play-2.5/swagger-play2/build.sbt +++ b/play-2.5/swagger-play2/build.sbt @@ -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", diff --git a/play-2.5/swagger-play2/test/PlayApiListingCacheSpec.scala b/play-2.5/swagger-play2/test/PlayApiListingCacheSpec.scala index bf9f0a0..2d64261 100644 --- a/play-2.5/swagger-play2/test/PlayApiListingCacheSpec.scala +++ b/play-2.5/swagger-play2/test/PlayApiListingCacheSpec.scala @@ -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 = { From bc0c0e8d481c1173d7c255d354685bc308f44705 Mon Sep 17 00:00:00 2001 From: Bernhard Altendorfer Date: Wed, 4 Apr 2018 14:00:11 +0200 Subject: [PATCH 4/4] Migrate cloning to 2.6 --- .../app/play/modules/swagger/ApiListingCache.scala | 9 +++++++-- play-2.6/swagger-play2/build.sbt | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/play-2.6/swagger-play2/app/play/modules/swagger/ApiListingCache.scala b/play-2.6/swagger-play2/app/play/modules/swagger/ApiListingCache.scala index a270cc4..40e5ce3 100644 --- a/play-2.6/swagger-play2/app/play/modules/swagger/ApiListingCache.scala +++ b/play-2.6/swagger-play2/app/play/modules/swagger/ApiListingCache.scala @@ -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 @@ -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") @@ -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 } } diff --git a/play-2.6/swagger-play2/build.sbt b/play-2.6/swagger-play2/build.sbt index a7ff1d0..9c9613c 100644 --- a/play-2.6/swagger-play2/build.sbt +++ b/play-2.6/swagger-play2/build.sbt @@ -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",