Giter Club home page Giter Club logo

swagger-gradle-plugin's Introduction

Swagger Gradle Plugin Build Status Download

This plugin was based on kongchen's swagger-maven-plugin

This enables your Swagger-annotated project to generate Swagger specs and customizable, templated static documents during the gradle build phase. Unlike swagger-core, swagger-gradle-plugin does not actively serve the spec with the rest of the application; it generates the spec as a build artifact to be used in downstream Swagger tooling.

N.B This plugin is tested against the oldest and latest of each major Gradle version from 3.2 onwards. The reason for 3.2 is that it's the first that supports Kotlin therefore keeping tests simpler. This does not mean that this plugin won't work with earlier versions, just your mileage may vary.

Features

Usage

Import the plugin in your project by adding following configuration:

Gradle version >= 2.1

plugins {
    id 'com.benjaminsproule.swagger' version '1.0.0'
}

Gradle versions < 2.1

buildscript {
  repositories {
    maven {
      url 'https://plugins.gradle.org/m2/'
    }
  }
  dependencies {
    classpath 'gradle.plugin.com.benjaminsproule:swagger-gradle-plugin:1.0.0'
  }
}

apply plugin: 'com.benjaminsproule.swagger'

swagger {
    apiSource {
        ...
    }
}

One apiSource can be considered as a version of APIs of your service.

You can specify several apiSources. Generally, one is enough.

swagger {
    apiSource {
        ...
    }
    apiSource {
        ...
    }
}

Configuration for swagger

name description
apiSources List of apiSource closures. One apiSource can be considered as a version of APIs of your service. You can specify several apiSource closures, though generally one is enough.

Configuration for apiSource

name description
springmvc Tell the plugin your project is a JAX-RS(false) or a SpringMvc(true) project
locations required Classes containing Swagger's annotation @Api, or packages containing those classes can be configured here, using ; as the delimiter.
schemes The transfer protocol of the API. Values MUST be from the list: "http", "https", "ws", "wss".
host The host (name or ip) serving the API. This MUST be the host only and does not include the scheme nor sub-paths. It MAY include a port. The host does not support path templating.
basePath The base path on which the API is served, which is relative to the host. The value MUST start with a leading slash (/). The basePath does not support path templating.
descriptionFile A Path to file with description to be set to Swagger Spec 2.0's info Object
info required The basic information of the api, using same definition as Swagger Spec 2.0's info Object
securityDefinitions You can put your security definitions here, see more details below
templatePath The path of a handlebars template file, see more details below.
outputPath The path of the generated static document, not existed parent directories will be created. If you don't want to generate a static document, just don't set it.
outputFormats The format types of the generated swagger spec. Valid values are json, yaml or both (as a list, e.g. ['json']). The json format is default.
swaggerDirectory The directory of generated swagger.json file. If null, no swagger.json will be generated.
swaggerFileName The filename of generated filename.json file. If null, swagger.json will be generated.
swaggerApiReader If not null, the value should be a full name of the class implementing com.github.kongchen.swagger.docgen.reader.ClassSwaggerReader. This allows you to flexibly implement/override the reader's implementation. Default is com.github.kongchen.swagger.docgen.reader.JaxrsReader. More details below
attachSwaggerArtifact If enabled, the generated swagger.json file will be attached as a gradle artifact. The swaggerDirectory's name will be used as an artifact classifier. Default is false.
modelSubstitute The model substitute file's path, see more details below
typesToSkip Nodes of class names to explicitly skip during parameter processing. More details below
apiModelPropertyAccessExclusionsList Allows the exclusion of specified @ApiModelProperty fields. This can be used to hide certain model properties from the swagger spec. More details below
jsonExampleValues If true, all example values in @ApiModelProperty will be handled as json raw values. This is useful for creating valid examples in the generated json for all property types, including non-string ones.
modelConverters List of custom implementations of io.swagger.converter.ModelConverter that should be used when generating the swagger files. More details below
excludePattern Regex of files that will be excluded from the swagger documentation. The default is .*\\.pom so it ignores all pom files.

If you'd like to generate a template-driven static document, such as markdown or HTML documentation, you'll need to specify a handlebars template file in templatePath. The value for templatePath supports 2 kinds of path:

  1. Resource in classpath. You should specify a resource path with a classpath: prefix. e.g:

    1. classpath:/markdown.hbs
    2. classpath:/templates/hello.html
  2. Local file's absolute path. e.g:

    1. ${project.rootDir}/src/main/resources/markdown.hbs
    2. ${project.rootDir}/src/main/resources/template/hello.html

There's a standalone project for the template files, fetch them and customize it for your own project.

There're 3 types of security definitions according to Swagger Spec: basic, apiKey and oauth2.

You can define multi definitions here, but you should fully follow the spec.

Basic - Security Definitions

You can define a basic definition like this:

securityDefinition {
    // `name` can be used refer to this security schemes from elsewhere
    name = 'MyBasicAuth'
    type = 'basic'
}

ApiKey - Security Definitions

You can also define a ApiKey definition like this:

swagger {
    apiSource {
        ...
        securityDefinition {
            // `name` can be used refer to this security schemes from elsewhere
            name = 'ApiKeyAuth'
            type = 'apiKey'
            // The location of the API key. Valid values are "query" or "header".
            keyLocation = 'header'
            // The name of the header
            keyName = 'X-API-Key'
        }
    }
}

Oauth2 - Security Definitions

You can also define a Oauth2 definition like this:

swagger {
    apiSource {
        ...
        securityDefinition {
            // `name` can be used refer to this security schemes from elsewhere
            name = 'OAuth2Authentication'
            type = 'oauth2'
            // The flow used by the OAuth2 security scheme
            flow = 'accessCode'
            authorizationUrl = 'https://somewhere.com/authorization'
            tokenUrl = 'https://somewhere.com/token'
            scope {
                name = 'read:model'
                description = 'Read the details of the model'
            }
        }
    }
}

Json - Security Definitions

It is also possible to define several definitions in a json file and specify the json path like this:

securityDefinition {
    json = 'securityDefinition.json'
}

Alternatively, specify the absolute file path to the json definition file:

securityDefinition {
    json = "${project.projectDir}/securityDefinition.json"
}

The securityDefinition.json file should also follow the spec, one sample file like this:

{
  "api_key": {
    "type": "apiKey",
    "name": "api_key",
    "in": "header"
  },
  "petstore_auth": {
    "type": "oauth2",
    "authorizationUrl": "http://swagger.io/api/oauth/dialog",
    "flow": "implicit",
    "scopes": {
      "write:pets": "modify pets in your account",
      "read:pets": "read your pets"
    }
  }
}

Note: It is only possible to define the OAuth2 type in a json file and not directly in the gradle configuration.

You can instruct swagger-gradle-plugin to use a custom swagger api reader rather than use the default by adding the following to your build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.custom:swagger-api-reader:1.0.0'
    }
}
...
swagger {
    apiSource {
        ...
        swaggerApiReader = [ 'com.custom.swagger.ApiReader' ]
    }
}

It is important to note that the class has to be available in the buildscript's classpath.

Throughout the course of working with Swagger, you may find that you need to substitute non-primitive objects for primitive objects. This is called model substituion, and it is supported by swagger-gradle-plugin. In order to configure model substitution, you'll need to create a model substitute file. This file is a simple text file containing n lines, where each line tells swagger-gradle-plugin to substitutes a model class with the supplied substitute. These two classes should be seperated by a colone (:).

Sample model substitution

com.foo.bar.PetName : java.lang.String

The above model substitution configuration would tell the plugin to substitute com.foo.bar.PetName with java.lang.String. As a result, the generated swagger.json would look like this ...

 "definitions" : {
    "Pet" : {
      "properties" : {
        ...
        "petName" : {
          "type" : "string"
        }
        ...
      }
    }

... instead of like this:

 "definitions" : {
    "Pet" : {
      "properties" : {
        ...
        "petName" : {
          "$ref" : "#/definitions/PetName"
        }
        ...
      }
    }

The model substitution file will be read by getClass().getResourceAsStream, so please note the path you configured.

You can instruct swagger-gradle-plugin to skip processing the parameters of certain types by adding the following to your build.gradle: // TODO: Not fully supported yet

typesToSkip = [
    'com.foobar.skipper.SkipThisClassPlease',
    'com.foobar.skipper.AlsoSkipThisClassPlease'
]

You can instruct swagger-gradle-plugin to use a custom model converter rather than use the default by adding the following to your build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.custom:model-converter:1.0.0'
    }
}
...
swagger {
    apiSource {
        ...
        modelConverters = [ 'com.custom.model.Converter' ]
    }
}

It is important to note that the class has to be available in the buildscript's classpath.

If you'd like to exclude certain @ApiModelPropertys based on their access values, you may do so by adding the following as a child node of apiSource in your build.gradle:

apiModelPropertyAccessExclusionsList = [
    'secret-property'
]

The above setting would prevent internalThing from appearing in the swagger spec output, given this annotated model:

...
    @ApiModelProperty(name = "internalThing", access = "secret-property")
    public String getInternalThing() {
        return internalThing;
    }
...

Note: In order to use apiModelPropertyAccessExclusionsList, you must specify both the name and access fields of the property you wish to exclude.

Generating the swagger documentation

To generate the swagger documentation, you need to run ./gradlew generateSwaggerDocumentation.

N.B In previous versions (< 0.1.0) the task was swagger, but this caused a conflict with another plugin, swagger-codegen, (issue #8).

Skipping generating the swagger documentation

To skip generating the swagger documentation, you need to include the property swagger.skip (e.g. ./gradlew clean build -Pswagger.skip)

Install/Deploy swagger.json

You can instruct swagger-gradle-plugin to deploy the generated swagger.json by adding the following to your build.gradle:

swaggerDirectory = "${project.rootDir}/swagger-ui"
attachSwaggerArtifact = true

The above setting attaches the generated file to Gradle for install/deploy purpose with swagger-uias classifier and json as type

Example

plugins {
    id "com.benjaminsproule.swagger" version "1.0.0"
}

swagger {
    apiSource {
        springmvc = true
        locations = [ 'com.wordnik.swagger.sample' ]
        schemes = [ 'http', 'https' ]
        host = 'www.example.com:8080'
        basePath = '/api'
        info {
            title = 'Swagger Gradle Plugin Sample'
            version = 'v1'
            // use markdown here because I'm using markdown for output,
            // if you need to use html or other markup language, you need to use your target language
            description = 'This is a sample.'
            termsOfService = 'http://www.example.com/termsOfService'
            contact {
                email = '[email protected]'
                name = 'Name'
                url = 'http://www.example.com'
            }
            license {
                url = 'http://www.apache.org/licenses/LICENSE-2.0.html'
                name = 'Apache 2.0'
            }
        }
        securityDefinition {
            name = 'basicAuth'
            type = 'basic'
        }
        securityDefinition {
            json = 'securityDefinition.json'
        }
        /**
            Support classpath or file absolute path here.
            1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
            2) file e.g: "${project.rootDir}/src/main/resources/markdown.hbs", "${project.rootDir}/src/main/resources/template/hello.html"
        **/
        templatePath = "${project.rootDir}/src/test/resources/strapdown.html.hbs"
        outputPath = "${project.rootDir}/generated/document.html"
        swaggerDirectory = "${project.rootDir}/generated/swagger-ui"
        swaggerApiReader = 'com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader'
        modelConverters = [ 'io.swagger.validator.BeanValidator' ]
        // attachSwaggerArtifact = true - WILL BE ADDED IN THE FUTURE
    }
}

To run integration tests

This plugin uses the gradle testkit, so requires the pluginUnderTestMetadata task to be run before hand.

./gradlew pluginUnderTestMetadata

To run tests against different versions of Gradle

This plugin will by default run the tests against Gradle 4.7. To change which version it runs tests against, test.gradleVersion system property is required.

./gradlew /gradlew clean test -Dtest.gradleVersion=4.0

To release

This plugin uses the gradle-release plugin, so to release the plugin.

In ~/.gradle/gradle.properties, bintray_user and bintray_apiKey need to be set for publishing to Bintray and gradle.publish.key and gradle.publish.secret for publishing to the central Gradle repository.

./gradlew release -Prelease.useAutomaticVersion=true

swagger-gradle-plugin's People

Contributors

gigasproule avatar istomisgood avatar fmotrifork avatar set321go avatar franzwong avatar groboclown avatar mousecage avatar

Watchers

James Cloos avatar Tasuku Tozawa 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.