diff --git a/play-2.6/swagger-play2/README.md b/play-2.6/swagger-play2/README.md index 92acf4f..bd32b12 100644 --- a/play-2.6/swagger-play2/README.md +++ b/play-2.6/swagger-play2/README.md @@ -122,6 +122,74 @@ swagger.api.info = { licenseUrl : (String) - Terms Of Service | default : empty } ``` +## Rendering SecurityDefinition + +To render SecurityDefinition you need to add *SecurityDefinition* in *SwaggerDefinition* and *Authorization* to method that will require Authorization +``` +@Singleton +@Api(value = "Customer") +@SwaggerDefinition( + securityDefinition = new SecurityDefinition( + apiKeyAuthDefintions = Array( + new ApiKeyAuthDefinition( + name = "Authorization", + key = "Bearer", + in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, + description="For Accessing the API must provide a valid JWT Token ") + ) + ) +) +class CustomerController @Inject() (val controllerComponents: ControllerComponents) extends BaseController { + + + @ApiOperation(value = "get All Customers", + nickname = "getAllCustomers", + notes = "Retuns a list of Customer", + response = classOf[Customer], + responseContainer = "List", + httpMethod = "GET", + authorizations = Array( + new Authorization("Bearer") + ) + ) +``` +By annotating coding as shown above swagger.json will contains necessary elementos to be used with swagger-ui +``` + "/customers" : { + "get" : { + "tags" : [ "Customer" ], + "summary" : "get All Customers", + "description" : "Retuns a list of Customer", + "operationId" : "getAllCustomers", + "parameters" : [ ], + "responses" : { + "200" : { + "description" : "successful operation", + "schema" : { + "type" : "array", + "items" : { + "$ref" : "#/definitions/Customer" + } + } + } + }, + "security" : [ { + "Bearer" : [ ] + } ] + } + } + }, + "securityDefinitions" : { + "Bearer" : { + "description" : "For Accessing the API must provide a valid JWT Token ", + "type" : "apiKey", + "name" : "Authorization", + "in" : "header" + } + }, +``` + +A great explanation about *Use Authorization Header with Swagger* can be found here http://www.mimiz.fr/blog/use-authorization-header-with-swagger/ ## Note on Dependency Injection This plugin works by default if your application uses Runtime dependency injection. diff --git a/play-2.6/swagger-play2/app/play/modules/swagger/PlayReader.java b/play-2.6/swagger-play2/app/play/modules/swagger/PlayReader.java index 1a91a3e..cf51ffc 100644 --- a/play-2.6/swagger-play2/app/play/modules/swagger/PlayReader.java +++ b/play-2.6/swagger-play2/app/play/modules/swagger/PlayReader.java @@ -8,6 +8,7 @@ import io.swagger.models.Contact; import io.swagger.models.ExternalDocs; import io.swagger.models.Tag; +import io.swagger.models.auth.In; import io.swagger.models.parameters.*; import io.swagger.models.parameters.Parameter; import io.swagger.models.properties.*; @@ -289,6 +290,17 @@ protected void readSwaggerConfig(Class cls, SwaggerDefinition config) { } } + + for(ApiKeyAuthDefinition apiKeyAuthConfig:config.securityDefinition().apiKeyAuthDefintions()){ + io.swagger.models.auth.ApiKeyAuthDefinition apiKeyAuthDefinition = new io.swagger.models.auth.ApiKeyAuthDefinition(); + + apiKeyAuthDefinition.setName(apiKeyAuthConfig.name()); + apiKeyAuthDefinition.setIn(In.forValue(apiKeyAuthConfig.in().toValue())); + apiKeyAuthDefinition.setDescription(apiKeyAuthConfig.description()); + + swagger.addSecurityDefinition(apiKeyAuthConfig.key(), apiKeyAuthDefinition); + } + for (SwaggerDefinition.Scheme scheme : config.schemes()) { if (scheme != SwaggerDefinition.Scheme.DEFAULT) { swagger.addScheme(Scheme.forValue(scheme.name())); @@ -856,4 +868,4 @@ public Property wrap(String container, Property property) { protected abstract Property doWrap(Property property); } -} +} \ No newline at end of file diff --git a/play-2.6/swagger-play2/app/play/modules/swagger/SwaggerPlugin.scala b/play-2.6/swagger-play2/app/play/modules/swagger/SwaggerPlugin.scala index ddbf73f..2b51209 100644 --- a/play-2.6/swagger-play2/app/play/modules/swagger/SwaggerPlugin.scala +++ b/play-2.6/swagger-play2/app/play/modules/swagger/SwaggerPlugin.scala @@ -39,45 +39,45 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route val config = app.configuration logger.info("Swagger - starting initialisation...") - val apiVersion = config.getString("api.version") match { + val apiVersion = config.getOptional[String]("api.version") match { case None => "beta" case Some(value) => value } - val basePath = config.getString("swagger.api.basepath") + val basePath = config.getOptional[String]("swagger.api.basepath") .filter(path => !path.isEmpty) .getOrElse("/") - val host = config.getString("swagger.api.host") + val host = config.getOptional[String]("swagger.api.host") .filter(host => !host.isEmpty) .getOrElse("localhost:9000") - val title = config.getString("swagger.api.info.title") match { + val title = config.getOptional[String]("swagger.api.info.title") match { case None => "" case Some(value)=> value } - val description = config.getString("swagger.api.info.description") match { + val description = config.getOptional[String]("swagger.api.info.description") match { case None => "" case Some(value)=> value } - val termsOfServiceUrl = config.getString("swagger.api.info.termsOfServiceUrl") match { + val termsOfServiceUrl = config.getOptional[String]("swagger.api.info.termsOfServiceUrl") match { case None => "" case Some(value)=> value } - val contact = config.getString("swagger.api.info.contact") match { + val contact = config.getOptional[String]("swagger.api.info.contact") match { case None => "" case Some(value)=> value } - val license = config.getString("swagger.api.info.license") match { + val license = config.getOptional[String]("swagger.api.info.license") match { case None => "" case Some(value)=> value } - val licenseUrl = config.getString("swagger.api.info.licenseUrl") match { + val licenseUrl = config.getOptional[String]("swagger.api.info.licenseUrl") match { // licenceUrl needs to be a valid URL to validate against schema case None => "http://licenseUrl" case Some(value)=> value @@ -110,7 +110,7 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route val routesFile = config.underlying.hasPath("play.http.router") match { case false => "routes" - case true => config.getString("play.http.router") match { + case true => config.getOptional[String]("play.http.router") match { case None => "routes" case Some(value)=> playRoutesClassNameToFileName(value) } @@ -147,7 +147,7 @@ class SwaggerPluginImpl @Inject()(lifecycle: ApplicationLifecycle, router: Route val route = new RouteWrapper(routesRules) RouteFactory.setRoute(route) - app.configuration.getString("swagger.filter") match { + app.configuration.getOptional[String]("swagger.filter") match { case Some(e) if (e != "") => { try { FilterFactory setFilter SwaggerContext.loadClass(e).newInstance.asInstanceOf[SwaggerSpecFilter] diff --git a/play-2.6/swagger-play2/build.sbt b/play-2.6/swagger-play2/build.sbt index a7ff1d0..4552524 100644 --- a/play-2.6/swagger-play2/build.sbt +++ b/play-2.6/swagger-play2/build.sbt @@ -1,9 +1,9 @@ name := "swagger-play2" -version := "1.6.1-SNAPSHOT" +version := "1.6.2-SNAPSHOT" checksums in update := Nil -scalaVersion := "2.11.8" +scalaVersion := "2.11.11" crossScalaVersions := Seq(scalaVersion.value, "2.12.2")