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

Remove deprecated getString from SwaggerPlugin and fix to render Secu… #162

Open
wants to merge 2 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
68 changes: 68 additions & 0 deletions play-2.6/swagger-play2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion play-2.6/swagger-play2/app/play/modules/swagger/PlayReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -856,4 +868,4 @@ public Property wrap(String container, Property property) {

protected abstract Property doWrap(Property property);
}
}
}
22 changes: 11 additions & 11 deletions play-2.6/swagger-play2/app/play/modules/swagger/SwaggerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions play-2.6/swagger-play2/build.sbt
Original file line number Diff line number Diff line change
@@ -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")

Expand Down