Skip to content

JAX-RS & SpringMVC supported gradle build plugin to generate Swagger documentation

License

Notifications You must be signed in to change notification settings

copa2/swagger-gradle-plugin

 
 

Repository files navigation

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
security A declaration of which security schemes are applied for the API as a whole. security requirement 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 swaggerFileName 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.

Allows to set a security requirement on the whole API. This can be done with multiple security requirements applied as AND or OR values. See https://swagger.io/docs/specification/2-0/authentication/ The key of the security requirement has to match a name from a securityDefinition.

security = [ [ ApiKeyAuth : [] ] ]

Use BasicAuth with ApiKey or OAuth2 with given scopes (basic(name: MyBasicAuth) && apiKey(name: MyApiKey)) || oauth2(name: MyOAuth2)

security = [ [ MyBasicAuth : [], MyApiKey : [] ], [ MyOAuth2 : [ 'scope1', 'scope2' ] ] ]

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:

attachSwaggerArtifact = true

The above setting attaches the generated file to Gradle for install/deploy purpose with swaggerDirectory's name as classifier and the outputFormat as type.

Please note that when using the maven-publish plugin instead of the maven plugin, the classifier must be specified in the configuration as it uses a different mechanism for the classifier. This is especially important when using multiple apiSource closures. Example:

publishing {
    publications {
        maven(MavenPublication) {
            artifact source: "${swaggerDirectory}/publicApiSwagger.json", classifier: 'publicApiSwagger'
            artifact source: "${swaggerDirectory}/privateApiSwagger.json", classifier: 'privateApiSwagger'
        }
    }
    repositories {
        maven {
            url "https://path/to/repo"
        }
    }
}
swagger {
     apiSource {
         attachSwaggerArtifact = true
         locations = ['com.benjaminsproule.public']
         swaggerDirectory = "${swaggerDirectory}"
         swaggerFileName = 'publicSwagger'
     }
     apiSource {
         attachSwaggerArtifact = true
         locations = ['com.benjaminsproule.private']
         swaggerDirectory = "${swaggerDirectory}"
         swaggerFileName = 'privateSwagger'
     }
}

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@email.com'
                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
    }
}

To run integration tests

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

./gradlew pluginUnderTestMetadata

N.B. for reliable test runs in an IDE, it's best to include this as a Gradle build step as part of the test run configuration.

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

About

JAX-RS & SpringMVC supported gradle build plugin to generate Swagger documentation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Groovy 85.8%
  • Java 6.4%
  • Kotlin 5.9%
  • HTML 1.6%
  • Shell 0.3%