Giter Club home page Giter Club logo

typeswagger's Introduction

TypeSwagger - A Swagger / OpenApi Spec DSL for Scala

License: MIT Build Status Codacy Badge

TypeSwagger provides a typesafe Scala DSL to build OpenApi (Swagger) Specifications. It can be used within any Scala project to generate HTTP API documentations.

Example

The following example defines a simple api with two endpoints (GET and DELETE) on one path (/user/{id}). The example contains mandatory information, and also some, but not all optional fields. For better readability, optional fields are calld by value.

import ch.acmesoftware.typeswagger.v3.OpenApi
import ch.acmesoftware.typeswagger.v3.OpenApi._
import ch.acmesoftware.typeswagger.v3.Implicits._

OpenApi.create("ApiDoc", "1.0.0").
  // add global information
  withInfo(license = "MIT", licenseUrl = "https://opensource.org/licenses/MIT").
  // add a tag
  withTag("testtag", "A tag description", externalDocs = doc("http://link.to.doc")).
  // define an api path
  path("/user/{id}", summary = "Path summary") {
    // http GET operation  on the path
    (GET >> op("A summary", "GET operation for this route").
      withParameter("id", PATH, Schema.int, description = "The id...", required = true).
      withParameter("comment", QUERY, Schema.string, description = "Some deprecated comment", 
                    deprecated = true).
      withTag("testtag")) ~
    // http DELETE operation on the path
    (DELETE >> op("Delete operation").
      withParameter("id", PATH, Schema.int, description = "The id...", required = true))
  }.
  // add endpoint server(s)
  withServer("http://localhost:9000/api").
  withServer("https://production.tld/api", Some("Production Server")).
  // build json string
  toJson()

Usage

Install the Ivy depenency via SBT:

"ch.acmesoftware" %% "typeswagger" % "{version}"

Add imports:

import ch.acmesoftware.typeswagger.v3.OpenApi
import ch.acmesoftware.typeswagger.v3.OpenApi._

// syntactic sugar for convenient DSL, but optional
import ch.acmesoftware.typeswagger.v3.Implicits._ 

Info

Add global information using the withInfo function. All parameters are optional. If not using the implicits, use Some("str") as parameter value:

import ch.acmesoftware.typeswagger.v3.OpenApi
import ch.acmesoftware.typeswagger.v3.OpenApi._
import ch.acmesoftware.typeswagger.v3.Implicits._

OpenApi.create("ApiDoc", "1.0.0", description = "API Doc built with TypeSwagger", termsOfService = "/tos").
  withInfo(license = "MIT", licenseUrl = "https://opensource.org/licenses/MIT", 
           contactName = "John Doe", contactEmail = "[email protected]", contactUrl = "https://webpage.tld")

Features

  • Convenient, typesafe DSL to define OpenApi v3 specifications
  • Integrated JSON rendering
  • Integrated YAML rendering (TODO)
  • Java API (TODO)
  • Integration with common HTTP-Frameworks (TODO)

Contributions

Please use the GitHub issue tracking and PullRequests. Any help is very welcome.

License

This project is licensed under the MIT license. See LICENSE for more information.

typeswagger's People

Contributors

frne avatar

Stargazers

 avatar

Watchers

 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.