Skip to content

Commit

Permalink
ref swagger-api#214 - Vendor Extensions support in API Info
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Ciuloaica committed Dec 27, 2019
1 parent 32d3d11 commit 7ddfaa3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ swagger.api {
contact = ""
license = ""
licenseUrl = "http://licenseUrl" # needs to be a valid url to validate against schema
x-api-key: "test"
x-tags: ["api", "secure"]
}
}
6 changes: 6 additions & 0 deletions src/main/scala/play/modules/swagger/PlayApiScanner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class PlayApiScanner @Inject()(config: PlaySwaggerConfig, routes: RouteWrapper,
.name(config.license)
.url(config.licenseUrl))
}

val vendorExtensions = config.vendorExtensions.map { extension =>
extension.name -> extension.value
}.toMap.asJava

info.setVendorExtensions(vendorExtensions)
swagger.info(info)
}

Expand Down
14 changes: 12 additions & 2 deletions src/main/scala/play/modules/swagger/PlaySwaggerConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import com.typesafe.config.Config
import javax.annotation.Nullable
import play.api.Configuration

import scala.collection.JavaConverters._

case class Extension(name: String, value: AnyRef)

case class PlaySwaggerConfig(
title: String,
version: String,
Expand All @@ -15,7 +19,8 @@ case class PlaySwaggerConfig(
host: String,
basePath: String,
schemes: Seq[String],
filterClass: Option[String]
filterClass: Option[String],
vendorExtensions: List[Extension]
) {
// Java APIs for reading the configuration
def getSchemes: Array[String] = schemes.toArray
Expand All @@ -37,7 +42,12 @@ object PlaySwaggerConfig {
termsOfServiceUrl = configuration.get[String]("swagger.api.info.termsOfServiceUrl"),
license = configuration.get[String]("swagger.api.info.license"),
licenseUrl = configuration.get[String]("swagger.api.info.licenseUrl"),
filterClass = configuration.get[Option[String]]("swagger.filter")
filterClass = configuration.get[Option[String]]("swagger.filter"),
vendorExtensions = configuration.get[Option[Config]]("swagger.api.info").toList.map(_.entrySet()).flatMap { entries =>
entries.asScala.filter(_.getKey.startsWith("x-")).map { entry =>
Extension(entry.getKey, entry.getValue.unwrapped())
}.toList.reverse
}
)
}

Expand Down
5 changes: 4 additions & 1 deletion src/test/scala/PlayApiListingCacheSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.io.File

import com.typesafe.config.ConfigValueFactory
import io.swagger.config.ScannerFactory
import io.swagger.models.{HttpMethod, ModelImpl}
import io.swagger.models.parameters.{BodyParameter, PathParameter, QueryParameter}
Expand Down Expand Up @@ -57,7 +58,8 @@ PUT /api/dog/api/:id testdata.DogController.add0(id:String)
license = "license",
licenseUrl = "http://licenseUrl",
filterClass = None,
schemes = Seq.empty
schemes = Seq.empty,
vendorExtensions = List(Extension("x-api-key","test"), Extension("x-tags", java.util.Arrays.asList("api", "secure","test")))
)

val env = Environment.simple()
Expand Down Expand Up @@ -90,6 +92,7 @@ PUT /api/dog/api/:id testdata.DogController.add0(id:String)
swagger.getInfo.getTermsOfService must beEqualTo(swaggerConfig.termsOfServiceUrl)
swagger.getInfo.getLicense.getName must beEqualTo(swaggerConfig.license)
swagger.getSecurityDefinitions.size() must beEqualTo(1)
swagger.getInfo.getVendorExtensions.size() must beEqualTo(2)

val pathDoc = swagger.getPaths.get("/document/{settlementId}/files/{fileId}/accept")
pathDoc.getOperations.size must beEqualTo(1)
Expand Down

0 comments on commit 7ddfaa3

Please sign in to comment.