# Swagger Gradle Plugin [![CircleCI](https://circleci.com/gh/gigaSproule/swagger-gradle-plugin.svg?style=svg)](https://circleci.com/gh/gigaSproule/swagger-gradle-plugin) [![Download](https://api.bintray.com/packages/gigasproule/maven/swagger-gradle-plugin/images/download.svg)](https://bintray.com/gigasproule/maven/swagger-gradle-plugin/_latestVersion)
This plugin was based on [kongchen's swagger-maven-plugin](https://github.com/kongchen/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 latest of each major Gradle version from 3.x onwards. The reason for 3.x, is that 3.2 was the first that supported Kotlin, therefore keeping tests simpler. This does _not_ mean that this plugin won't work with earlier versions, just your mileage may vary.
# Features
* Supports [Swagger Spec 2.0](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md)
* Supports [SpringMvc](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html) & [JAX-RS](https://jax-rs-spec.java.net/)
* Use [Handlebars](http://handlebarsjs.com/) as template to customize the static document.
# Usage
Import the plugin in your project by adding following configuration:
## Gradle version >= 2.1
### build.gradle (Groovy DSL)
```groovy
plugins {
id 'com.benjaminsproule.swagger' version '1.0.8'
}
```
### build.gradle.kts (Kotlin DSL)
```kotlin
plugins {
id("com.benjaminsproule.swagger") version "1.0.8"
}
```
## Gradle versions < 2.1
```groovy
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 `apiSource`s. Generally, one is enough.
```groovy
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](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#pathTemplating).|
| `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](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#pathTemplating). |
| `descriptionFile` | A Path to file with description to be set to Swagger Spec 2.0's [info Object](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#infoObject) |
| `info` **required**| The basic information of the api, using same definition as Swagger Spec 2.0's [info Object](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#infoObject) |
| `securityDefinitions` | You can put your [security definitions](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#security-definitions-object) here, see more details [below](#securityDefinitions)|
| `security` | A declaration of which security schemes are applied for the API as a whole. [security requirement](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#securityRequirementObject) see more details [below](#security)|
| `templatePath` | The path of a [handlebars](http://handlebarsjs.com/) template file, see more details [below](#templatefile).|
| `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.|
| `shouldSortArrays` | JSON arrays will be sorted instead of being written in the order of appearance. Default is `false`.|
| `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 extending `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](#swaggerApiReader)|
| `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](#model-substitution)|
| `typesToSkip` | Nodes of class names to explicitly skip during parameter processing. More details [below](#typesToSkip)|
| `apiModelPropertyAccessExclusionsList` | Allows the exclusion of specified `@ApiModelProperty` fields. This can be used to hide certain model properties from the swagger spec. More details [below](#apiModelPropertyAccessExclusionsList)|
| `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](#modelConverters)|
| `excludePattern` | Regex of files that will be excluded from the swagger documentation. The default is `.*\\.pom` so it ignores all pom files. |
| `tagStrategy` | Default no. `class` use class name if no tags are set to group operations specific to controller. (currently only springmvc) |
| `expandSuperTypes` | Default `true`. You can skip the scan of super types (parent class, interfarce)
# <a id="templatefile">Template File</a>
If you'd like to generate a template-driven static document, such as markdown or HTML documentation, you'll need to specify a [handlebars](https://github.com/jknack/handlebars.java) 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`**
1. **`classpath:/templates/hello.html`**
1. Local file's absolute path.
e.g:
1. **`${project.rootDir}/src/m