Swagger Play2 Module

build Maven Central

This fork is cross-compiled for Play 2.7 - 3.0

// for Play 2.7.3 - 2.7.9:
libraryDependencies += "com.github.dwickern" %% "swagger-play2.7" % "4.0.0"

// for Play 2.8.0 - 2.8.7:
libraryDependencies += "com.github.dwickern" %% "swagger-play2.8" % "4.0.0"

// for Play 2.8.8 - 2.8.x:
libraryDependencies ++= Seq(
  "com.github.dwickern" %% "swagger-play2.8" % "4.0.0",
  "io.swagger" % "swagger-core" % "1.6.2",
  "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.1"
)

// for Play 2.9:
libraryDependencies += "com.github.dwickern" %% "swagger-play2.9" % "4.0.0"

// for Play 3.0:
libraryDependencies += "com.github.dwickern" %% "swagger-play3.0" % "4.0.0"

Note that Jackson is generally not binary compatible between minor releases. Make sure all of your dependencies (including other third-party libraries) use a compatible version of Jackson:

playframework jackson swagger-core
2.7 2.9.x 1.5.24
2.8.0 - 2.8.7 2.10.x 1.6.0 - 1.6.1
2.8.8 - 2.8.x 2.11.x 1.6.2 - 1.6.5
N/A 2.12.x N/A
N/A 2.13.x 1.6.6 - 1.6.8
2.9.0 - 3.0.1 2.14.x 1.6.9 - 1.6.11
N/A 2.15.x 1.6.12
N/A 2.16.x 1.6.13

This library is also available as a SBT plugin: sbt-swagger-play

Overview

This is a module to support Swagger annotations within Play Framework controllers. It is based on the library https://github.com/swagger-api/swagger-play with several improvements. It can be used for both Scala and Java based applications.

We also would like to support Swagger 2.0 in the future and contributions to that end will be gladly accepted.

New and Noteworthy

  • Minimal dependencies: only depends on the core Play module, so it won't bring unnecessary dependencies on the Akka HTTP server or anything else from Play.
  • SwaggerPlugin no longer depends on on Application.
  • Correct Content-Length generation for JSON (originally proposed in swagger-api#176)
  • No longer uses deprecated Play configuration methods (proposed in swagger-api#162). Also uses reference.conf for default values.
  • Clarifies compile-time DI docs (proposed in swagger-api#157)
  • Handle route delegation properly (swagger-api#132 updated for Play 2.6)
  • Add support for dataTypeClass in ApiImplicitParam (swagger-api#174)
  • Add support for API keys (swagger-api#117)
  • Add support for OAuth2 (swagger-api#183)

Usage

You can depend on pre-built libraries in maven central by adding the following dependency:

(see above)

Or you can build from source.

sbt publishLocal

Adding Swagger to your Play2 app

There are just a couple steps to integrate your Play2 app with swagger.

1. Add the Swagger module to your application.conf

play.modules.enabled += "play.modules.swagger.SwaggerModule"

2. Add the resource listing to your routes file (you can read more about the resource listing here)


GET     /swagger.json           controllers.ApiHelpController.getResources

3. Annotate your REST endpoints with Swagger annotations. This allows the Swagger framework to create the api-declaration automatically!

In your controller for, say your "pet" resource:

  @ApiResponses(Array(
    new ApiResponse(code = 400, message = "Invalid ID supplied"),
    new ApiResponse(code = 404, message = "Pet not found")))
  def getPetById(
    @ApiParam(value = "ID of the pet to fetch") id: String) = Action {
    implicit request =>
      petData.getPetbyId(getLong(0, 100000, 0, id)) match {
        case Some(pet) => JsonResponse(pet)
        case _ => JsonResponse(new value.ApiResponse(404, "Pet not found"), 404)
      }
  }

What this does is the following:

  • Tells swagger that the methods in this controller should be described under the /api-docs/pet path

  • The Routes file tells swagger that this API listens to /{id}

  • Describes the operation as a GET with the documentation Find pet by Id with more detailed notes Returns a pet ....

  • Takes the param id, which is a datatype string and a path param

  • Returns error codes 400 and 404, with the messages provided

In the routes file, you then wire this api as follows:

GET     /pet/:id                 controllers.PetApiController.getPetById(id)

This will "attach" the /api-docs/pet api to the swagger resource listing, and the method to the getPetById method above

Please note that the minimum configuration needed to have a route/controller be exposed in swagger declaration is to have an Api annotation at class level.

The ApiParam annotation

Swagger for play has two types of ApiParams--they are ApiParam and ApiImplicitParam. The distinction is that some paramaters (variables) are passed to the method implicitly by the framework. ALL body parameters need to be described with ApiImplicitParam annotations. If they are queryParams or pathParams, you can use ApiParam annotations.

application.conf - config options

api.version (String) - version of API | default: "beta"
swagger.api.basepath (String) - base url | default: "http://localhost:9000"
swagger.filter (String) - classname of swagger filter | default: empty
swagger.api.info = {
  contact : (String) - Contact Information | default : empty,
  description : (String) - Description | default : empty,
  title : (String) - Title | default : empty,
  termsOfService : (String) - Terms Of Service | default : empty,
  license : (String) - Terms Of Service | default : empty,
  licenseUrl : (String) - Terms Of Service | default : empty
}

Note on Dependency Injection

This plugin works by default if your application uses Runtime dependency injection.

Nevertheless, the plugin can be initialized using compile time dependency injection. For example, you can add the following to your class that extends BuiltInComponentsFromContext:

// This needs to be eagerly instantiated because this sets global state for swagger
val swaggerPlugin = new SwaggerPluginImpl(environment, configuration)
lazy val apiHelpController = new ApiHelpController(controllerComponents, swaggerPlugin)

Security contact

Please disclose any security-related issues or vulnerabilities by emailing [email protected], instead of using the public issue tracker.