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

Commit

Permalink
ref #108, ref #182 - security definitions support and versions updates
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Apr 8, 2019
1 parent e39f0ea commit e52e4c6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cache:
language: scala

scala:
- 2.11.8
- 2.11.12

script:
- cd play-2.4/swagger-play2 && sbt ++$TRAVIS_SCALA_VERSION compile test
Expand All @@ -24,4 +24,4 @@ script:
- find $HOME/.sbt -name "*.lock" | xargs rm

jdk:
- oraclejdk8
- openjdk8
27 changes: 27 additions & 0 deletions 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 @@ -298,6 +299,32 @@ protected void readSwaggerConfig(Class<?> cls, SwaggerDefinition config) {
}
}

for (OAuth2Definition oAuth2DefinitionConfig:config.securityDefinition().oAuth2Definitions()) {

io.swagger.models.auth.OAuth2Definition oAuth2Definition = new io.swagger.models.auth.OAuth2Definition();
oAuth2Definition.setTokenUrl(oAuth2DefinitionConfig.tokenUrl());
oAuth2Definition.setAuthorizationUrl(oAuth2DefinitionConfig.authorizationUrl());
oAuth2Definition.setFlow(oAuth2DefinitionConfig.flow().name().toLowerCase());

for (Scope scope: oAuth2DefinitionConfig.scopes()) {
oAuth2Definition.addScope(scope.name(), scope.description());
}

swagger.addSecurityDefinition(oAuth2DefinitionConfig.key(), oAuth2Definition);
}

for (ApiKeyAuthDefinition apiKeyAuthConfig:config.securityDefinition().apiKeyAuthDefinitions()) {

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
21 changes: 11 additions & 10 deletions play-2.6/swagger-play2/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ version := "1.6.1-SNAPSHOT"

checksums in update := Nil

scalaVersion := "2.11.8"
scalaVersion := "2.11.12"

crossScalaVersions := Seq(scalaVersion.value, "2.12.2")
crossScalaVersions := Seq(scalaVersion.value, "2.12.6")

libraryDependencies ++= Seq(
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.9",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.9.8",
"org.slf4j" % "slf4j-api" % "1.7.21",
"io.swagger" % "swagger-core" % "1.5.16",
"io.swagger" %% "swagger-scala-module" % "1.0.5-SNAPSHOT",
"io.swagger" % "swagger-core" % "1.5.22",
"io.swagger" %% "swagger-scala-module" % "1.0.5",
"com.typesafe.play" %% "routes-compiler" % "2.6.0",
"com.typesafe.play" %% "play-ebean" % "4.0.2" % "test",
"org.specs2" %% "specs2-core" % "3.8.7" % "test",
Expand All @@ -21,13 +21,14 @@ libraryDependencies ++= Seq(

mappings in (Compile, packageBin) ~= { _.filter(!_._1.getName.equals("logback.xml")) }

publishTo <<= version { (v: String) =>
val nexus = "https://oss.sonatype.org/"
if (v.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
publishTo := {
if (version.value.trim.endsWith("SNAPSHOT"))
Some("Sonatype Nexus Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
Some("Sonatype Nexus Releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2")
}


publishArtifact in Test := false
publishMavenStyle := true
pomIncludeRepository := { x => false }
Expand Down
2 changes: 1 addition & 1 deletion play-2.6/swagger-play2/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.13
sbt.version=1.1.2
4 changes: 2 additions & 2 deletions play-2.6/swagger-play2/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.0")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.6")

addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1")
4 changes: 4 additions & 0 deletions play-2.6/swagger-play2/test/PlayApiListingCacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ PUT /api/dog/api/:id testdata.DogController.add0(id:String)
swagger.get.getInfo.getTitle must beEqualTo(swaggerConfig.getTitle)
swagger.get.getInfo.getTermsOfService must beEqualTo(swaggerConfig.getTermsOfServiceUrl)
swagger.get.getInfo.getLicense.getName must beEqualTo(swaggerConfig.getLicense)
swagger.get.getSecurityDefinitions.size() must beEqualTo(2)

val pathDoc = swagger.get.getPaths.get("/document/{settlementId}/files/{fileId}/accept")
pathDoc.getOperations.size must beEqualTo(1)
Expand Down Expand Up @@ -126,6 +127,8 @@ PUT /api/dog/api/:id testdata.DogController.add0(id:String)
opCatGet.getConsumes must beNull
opCatGet.getResponses.get("200").getSchema.asInstanceOf[ArrayProperty].getItems.asInstanceOf[RefProperty].getSimpleRef must beEqualTo("Cat")
opCatGet.getProduces must beNull
opCatGet.getSecurity.get(0).get("oauth2").get(0) must beEqualTo("write_pets")


val opCatPut = pathCat.getOperationMap.get(HttpMethod.PUT)
opCatPut.getOperationId must beEqualTo("add1")
Expand All @@ -135,6 +138,7 @@ PUT /api/dog/api/:id testdata.DogController.add0(id:String)
opCatPut.getConsumes must beNull
opCatPut.getResponses.get("200").getSchema.asInstanceOf[RefProperty].getSimpleRef must beEqualTo("ActionAnyContent")
opCatPut.getProduces must beNull
opCatPut.getSecurity.get(0).get("api_key") must not beNull

val pathCat43 = swagger.get.getPaths.get("/api/cat43")
pathCat43.getOperations.size must beEqualTo(1)
Expand Down
21 changes: 19 additions & 2 deletions play-2.6/swagger-play2/test/testdata/CatController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,28 @@ import io.swagger.annotations._

import play.api.mvc.{Action, Controller}

@SwaggerDefinition(
securityDefinition = new SecurityDefinition(
apiKeyAuthDefinitions = Array(
new ApiKeyAuthDefinition(name= "api_key", key="api_key", in=ApiKeyAuthDefinition.ApiKeyLocation.HEADER)
),
oAuth2Definitions = Array(
new OAuth2Definition(
key = "oauth2",
flow = OAuth2Definition.Flow.APPLICATION,
tokenUrl= "/oauth/token",
authorizationUrl = "/authorize",
scopes = Array(new Scope(name = "write_pets", description = "modify pets"))
)
)
)
)
@Api(value = "/apitest/cats", description = "play with cats")
class CatController extends Controller {

@ApiOperation(value = "addCat1",
httpMethod = "PUT",
authorizations = Array(),
authorizations = Array(new Authorization(value="api_key")),
consumes = "",
protocols = "")
@ApiImplicitParams(Array(
Expand Down Expand Up @@ -46,7 +62,8 @@ class CatController extends Controller {
notes = "Returns all cats",
response = classOf[Cat],
responseContainer = "List",
httpMethod = "GET")
httpMethod = "GET",
authorizations = Array(new Authorization(value="oauth2", scopes = Array(new AuthorizationScope(scope = "write_pets", description = "Modify pets")))))
@Deprecated
def list = Action {
request => Ok("test case")
Expand Down

0 comments on commit e52e4c6

Please sign in to comment.