• This repository has been archived on 15/Sep/2022
  • Stars
    star
    329
  • Rank 123,147 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Archived

This project is no longer supported.

Build Status

Swagger Play2 Module

Note

This branch (master) holds the latest version (major version 2.x) for latest play version supported (2.7); branch play-2.6 holds the swagger-play version for play 2.6 version (major.minor version 1.6.x).

Older versions are available though not anymore supported in the archive branch.

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. This library uses Swagger 1.5 and Play 2.7. 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 #176)
  • No longer uses deprecated Play configuration methods (proposed in #162). Also uses reference.conf for default values.
  • Clarifies compile-time DI docs (proposed in #157)
  • Handle route delegation properly (#132 updated for Play 2.6)
  • Add support for dataTypeClass in ApiImplicitParam (#174)
  • Add support for API keys (#117)
  • Add support for OAuth2 (#183)

Usage

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

libraryDependencies ++= Seq(
  "io.swagger" %% "swagger-play2" % "2.0.1-SNAPSHOT"
)

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.

More Repositories

1

swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
JavaScript
25,331
star
2

swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
Mustache
16,413
star
3

swagger-editor

Swagger Editor
JavaScript
8,614
star
4

swagger-core

Examples and server integrations for generating the Swagger API Specification, which enables easy access to your REST API
Java
7,323
star
5

swagger-node

Swagger module for node.js
JavaScript
3,970
star
6

swagger-js

Javascript library to connect to swagger-enabled APIs via browser or nodejs
JavaScript
2,574
star
7

swagger.io

The content of swagger.io
1,548
star
8

swagger-parser

Swagger Spec to Java POJOs
Java
744
star
9

swagger-samples

Samples for the various Swagger projects under swagger-api
JavaScript
536
star
10

swagger-socket

Swagger Socket: A REST over WebSocket
Java
381
star
11

swagger-codegen-generators

Mustache
277
star
12

swagger-petstore

Java
214
star
13

validator-badge

Validate your Swagger JSON/YAML today!
Java
201
star
14

swagger-inflector

Java
160
star
15

swagger-scala-module

Swagger support for scala
Scala
104
star
16

swagger-converter

OpenAPI/Swagger 2.0 to OpenAPI 3.0 Converter WebService
Python
97
star
17

apidom

Semantic parser for API specifications
TypeScript
55
star
18

swaggerhub-maven-plugin

A simple maven plugin to access SwaggerHub hosting of OpenAPI/Swagger from a maven build process.
Java
30
star
19

swagger-play-sample-app

A sample play app which uses swagger plugin to make the age old pet store swagger compliant.
JavaScript
29
star
20

rails-petstore

Ruby
29
star
21

swagger2

For working out the Swagger 2 working group page
CSS
23
star
22

swaggerhub-gradle-plugin

Gradle plugin for SwaggerHub
Java
20
star
23

scalatra-sample-app

Shell
20
star
24

swagger-scala-sample-app

A fully-functioning, stand-alone Swagger server written in scala which demonstrates how to enable Swagger in your API.
Scala
20
star
25

swagger-form-editor

JavaScript
19
star
26

petstore-kafka

A demo site built on top of Kafka topics
JavaScript
14
star
27

swagger-async-httpclient

Scala
10
star
28

swagger-editor-cra

This is forked Create React App that builds SwaggerEditor@5
JavaScript
9
star
29

swagger-scala

Scala
8
star
30

swagger-play-1.2

Scala
8
star
31

swagger-schema-packaging

6
star
32

apidom-lsp-vscode

ApiDOM VS Code Extension
TypeScript
5
star
33

sway-worker

4
star
34

apidom-ls-client

Demo client for apidom-ls OpenAPI / AsyncAPI ApiDOM validation service
TypeScript
3
star
35

.github

Common Github files for the Swagger projects
3
star
36

swagger-codegen-test

2
star