GithubHelp home page GithubHelp logo

swagger-scala's Introduction

swagger-scala

This implementation of swagger used to live in scalatra, it got extracted so it could be shared by more than one project.

How to use?

The main implementation of a swagger integration requires an implementation of com.wordnik.SwaggerEngine

Here is the implementation of that class for the default scalatra implementation

trait ScalatraSwaggerEngine[A <: SwaggerApi[_]] extends SwaggerEngine[A] {
  
  /**
   * Registers the documentation for an API with the given path.
   */
  def register(listingPath: String, resourcePath: String, description: Option[String], s: SwaggerSupportSyntax with SwaggerSupportBase, consumes: List[String], produces: List[String], protocols: List[String], authorizations: List[String])

}

/**
 * An instance of this class is used to hold the API documentation.
 */
class MySwagger(val apiVersion: String, val apiInfo: ApiInfo) extends ScalatraSwaggerEngine[Api] {
  
  val swaggerVersion = com.wordnik.swagger.Swagger.SpecVersion
  
  private[this] val logger = Logger[this.type]

  /**
   * Registers the documentation for an API with the given path.
   */
  def register(listingPath: String, resourcePath: String, description: Option[String], s: SwaggerSupportSyntax with SwaggerSupportBase, consumes: List[String], produces: List[String], protocols: List[String], authorizations: List[String]) = {
    logger.debug(s"registering swagger api with: { listingPath: $listingPath, resourcePath: $resourcePath, description: $resourcePath, servlet: ${s.getClass} }")
    val endpoints: List[Endpoint] = s.endpoints(resourcePath) collect { case m: Endpoint => m }
    _docs += listingPath -> Api(
      apiVersion,
      swaggerVersion,
      resourcePath,
      description,
      (produces ::: endpoints.flatMap(_.operations.flatMap(_.produces))).distinct,
      (consumes ::: endpoints.flatMap(_.operations.flatMap(_.consumes))).distinct,
      (protocols ::: endpoints.flatMap(_.operations.flatMap(_.protocols))).distinct,
      endpoints,
      s.models.toMap,
      (authorizations ::: endpoints.flatMap(_.operations.flatMap(_.authorizations))).distinct,
      0)
  }
}

This allows you to build the meta data necessary to generate a swagger api description. When you do generate the json as api description you should make use of the json formats provided in com.wordnik.swagger.SwaggerSerializers.defaultFormats

For example to render the index json you can use this method:

implicit formats: SwaggerFormats  = com.wordnik.swagger.SwaggerSerializers.defaultFormats
val mySwagger = new MySwagger(apiVersion, apiInfo)
Swagger.renderIndex(mySwagger, path => "/" + path)

And to render the description of a single doc you can use this:

implicit formats: SwaggerFormats  = com.wordnik.swagger.SwaggerSerializers.defaultFormats
val mySwagger = new MySwagger(apiVersion, apiInfo)
mySwagger.doc("/users").fold(JNothing)(Swagger.renderDoc(mySwagger, _, "/")) 

swagger-scala's People

Contributors

casualjim avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.