Swagger Maven Plugin

A maven build plugin which helps you generate API document during build phase

License

License

Categories

Categories

Maven Build Tools Swagger Program Interface REST Frameworks
GroupId

GroupId

io.github.cyrilschumacher
ArtifactId

ArtifactId

swagger-maven-plugin
Last Version

Last Version

3.1.11
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

Swagger Maven Plugin
A maven build plugin which helps you generate API document during build phase
Project URL

Project URL

https://github.com/kongchen/swagger-maven-plugin
Source Code Management

Source Code Management

https://github.com/cyrilschumacher/swagger-maven-plugin

Download swagger-maven-plugin

How to add to project

<plugin>
    <groupId>io.github.cyrilschumacher</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.11</version>
</plugin>

Dependencies

compile (20)

Group / Artifact Type Version
org.apache.maven : maven-artifact jar 2.2.1
org.apache.maven : maven-plugin-api jar 2.2.1
org.apache.maven : maven-project jar 2.2.1
org.apache.maven : maven-model jar 2.2.1
org.apache.maven : maven-core jar 2.2.1
com.github.jknack : handlebars jar 1.3.2
io.github.cyrilschumacher : swagger-jaxrs jar 1.6.4
io.github.cyrilschumacher : swagger-servlet jar 1.6.4
org.reflections : reflections jar 0.9.9
commons-io : commons-io jar 2.8.0
log4j : log4j jar 1.2.17
org.apache.commons : commons-lang3 jar 3.12.0
jakarta.ws.rs : jakarta.ws.rs-api jar 3.0.0
io.github.cyrilschumacher : swagger-jersey-jaxrs jar 1.6.4
com.sun.jersey : jersey-server jar 1.19.4
org.springframework : spring-context jar 4.3.7.RELEASE
org.springframework : spring-web jar 4.3.7.RELEASE
com.fasterxml.jackson.core : jackson-core jar 2.12.3
com.fasterxml.jackson.core : jackson-databind jar 2.12.3
com.fasterxml.jackson.module : jackson-module-jaxb-annotations jar 2.12.3

provided (1)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.6.0

test (11)

Group / Artifact Type Version
javax.xml.bind : jaxb-api jar 2.3.1
com.sun.xml.bind : jaxb-impl jar 2.3.1
org.testng : testng jar 6.14.3
org.apache.maven.plugin-testing : maven-plugin-testing-harness jar 1.3
net.javacrumbs.json-unit : json-unit jar 1.31.1
net.javacrumbs.json-unit : json-unit-core jar 1.31.1
org.codehaus.jettison : jettison jar 1.4.1
org.mockito : mockito-core jar 2.28.2
org.hamcrest : hamcrest-library jar 1.3
org.slf4j : slf4j-nop jar 1.7.25
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.61

Project Modules

There are no modules declared in this project.

Swagger Maven Plugin

Build Status Sonatype Nexus (Releases)

Fork of the swagger-maven-plugin project. This fork proposes an upgrade of Java EE.

This plugin enables your Swagger-annotated project to generate Swagger specs and customizable, templated static documents during the maven build phase. Unlike swagger-core, swagger-maven-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.

Features

Versions

  • 3.1.0 supports Swagger Spec 2.0, support JAX-RS & SpingMVC. (ACTIVE!)
  • 3.0.1 supports Swagger Spec 2.0, support JAX-RS & SpingMVC. (ACTIVE!)
  • 2.3.4 supports Swagger Spec 1.2, support JAX-RS & SpringMVC. (Lazily maintained)
  • 1.1.1 supports Swagger Spec 1.1. (No longer maintained)

Upgrading from 3.0.1 to 3.1.0+

Version 3.1.0+ of this plugin depends on the re-packaged/re-branded io.swagger.swagger-core dependency, which is formerly known as com.wordnik.swagger-core. If you use 3.1.0+, you must use the swagger-core dependency in the io.swagger namespace instead of the com.wordnik namespace, which is deprecated. You may see an example of migrating a project from 3.0.1 to 3.1.0 in the swagger-maven-plugin example project.

Usage

Import the plugin in your project by adding following configuration in your plugins block:

<build>
	<plugins>
		<plugin>
			<groupId>com.github.kongchen</groupId>
			<artifactId>swagger-maven-plugin</artifactId>
			<version>${swagger-maven-plugin-version}</version>
			<configuration>
				<apiSources>
					<apiSource>
						...
					</apiSource>
				</apiSources>
			</configuration>
			<executions>
				<execution>
					<phase>compile</phase>
					<goals>
						<goal>generate</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

The executions block is used to specify the phase of the build lifecycle you want the plugin to be executed in.

Configuration for configuration

name description
skipSwaggerGeneration If true, swagger generation will be skipped. Default is false. User property is swagger.skip.
apiSources List of apiSource elements. One apiSource can be considered as a version of APIs of your service. You can specify several apiSource elements, 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. Each item must be located inside a tag. Example: <locations><location>com.github.kongchen.swagger.sample.wordnik.resource</location><location>com.github.kongchen.swagger.sample.wordnik.resource2</location></locations>
schemes The transfer protocol of the API. Values MUST be from the list: "http", "https", "ws", "wss". Each value must be located inside its own <scheme> tag. Example: <schemes><scheme>http</scheme><scheme>https</scheme></schemes>
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 json,yaml. 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. com.github.kongchen.swagger.docgen.reader.SwaggerReader can be used to strictly use the official Swagger reader in order to generate the exact same output as Swagger''s runtime generation (with all its bugs). Default is com.github.kongchen.swagger.docgen.reader.JaxrsReader.
attachSwaggerArtifact If enabled, the generated swagger.json file will be attached as a maven 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
apiModelPropertyAccessExclusions 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.
swaggerExtensions List of custom implementations of io.swagger.jaxrs.ext.SwaggerExtension that should be used when generating the swagger files.
enabledObjectMapperFeatures List of ConfigFeature enums that are supported by ObjectMapper.configure - the feature is set to true. https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#features) here, see more details below
disabledObjectMapperFeatures List of ConfigFeature enums that are supported by ObjectMapper.configure - the feature is set to false. https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#features) here, see more details below
operationIdFormat Format of operationId used in Swagger spec. For historical reasons default is Java method name. Since 3.1.8, for new APIs suggested format is: {{className}}_{{methodName}}_{{httpMethod}}. {{packageName}} token is also supported.
externalDocs URL Reference to external documentation
responseMessageOverrides Default response message overrides of type @ApiResponse. Example: <responseMessageOverrides><responseMessageOverride><code>401</code><message>Unauthenticated - could not authenticate the user.</message></responseMessageOverride></responseMessageOverrides>

Template File

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. ${basedir}/src/main/resources/markdown.hbs
    2. ${basedir}/src/main/resources/template/hello.html

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

Security Definitions

There are 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.

You can define a basic definition like this:

<securityDefinition>
    <name>MybasicAuth</name>
    <type>basic</type>
</securityDefinition>

or define several definitions in a json file and specify the json path like this:

<securityDefinition>
    <json>/securityDefinition.json</json>
</securityDefinition>

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

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

<securityDefinition>
    <jsonPath>${basedir}/securityDefinition.json</jsonPath>
</securityDefinition>

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"
    }
  }
}

Model Substitution

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 substitution, and it is supported by swagger-maven-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-maven-plugin to substitutes a model class with the supplied substitute. These two classes should be separated by a colon (:).

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.

Skipping Types During Processing with typesToSkip

You can instruct swagger-maven-plugin to skip processing the parameters of certain types by adding the following to your pom.xml:

<typesToSkip>
  <typeToSkip>com.foobar.skipper.SkipThisClassPlease</typeToSkip>
  <typeToSkip>com.foobar.skipper.AlsoSkipThisClassPlease</typeToSkip>
</typesToSkip>

This requires at least swagger-maven-plugin version 3.1.1-SNAPSHOT.

Excluding certain @ApiModelProperty items

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 pom.xml:

<apiModelPropertyAccessExclusions>
    <apiModelPropertyAccessExclusion>secret-property</apiModelPropertyAccessExclusion>
</apiModelPropertyAccessExclusions>

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 apiModelPropertyAccessExclusions, you must specify both the name and access fields of the property you wish to exclude. Additionally, apiModelPropertyAccessExclusions requires at least swagger-maven-plugin version 3.1.1-SNAPSHOT.

Defining common Swagger parameters with JAX-RS annotations

When defining the Swagger API with JAX-RS, it is possible to specify common parameters by defining an @Api class that only contains fields annotated with JAX-RS parameter annotations. Note that this class should not contain any @Path or HTTP method annotation in order to consider it as a declaration of common parameters. When a common parameter is specified in another JAX-RS resource class, the parameter will have a reference to the common parameter.

The result of having common parameters and parameter references can be seen in the test file swagger-common-parameters.json. The @Api class used to generate this example can be seen in the test file JaxrsReaderTest.java.

Install/Deploy swagger.json

You can instruct swagger-maven-plugin to deploy the generated swagger.json by adding the following to your pom.xml:

<swaggerDirectory>${project.build.directory}/swagger-ui</swaggerDirectory>
<attachSwaggerArtifact>true</attachSwaggerArtifact>

or custom.json by adding the following to your pom.xml:

<swaggerDirectory>${project.build.directory}/swagger-ui</swaggerDirectory>
<swaggerFileName>custom</swaggerFileName>
<attachSwaggerArtifact>true</attachSwaggerArtifact>

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

Object Mapper Configuration Features

Enables or disables the static Json.mapper config, by setting the feature of a known enum to true or false respectively.

N.B. Inner class fully qualified domain names are using $

<configuration>
...
   <enabledObjectMapperFeatures>
       <feature>com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING</feature>
       <feature>com.fasterxml.jackson.core.JsonParser$Feature.ALLOW_NUMERIC_LEADING_ZEROS</feature>
   </enabledObjectMapperFeatures>
...
    <disabledObjectMapperFeatures>
        <feature>com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS</feature>
    </disabledObjectMapperFeatures>
</configuration>

Example

There's a sample here, just fork it and have a try.

A Sample Configuration

<project>
...
<build>
<plugins>
<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <apiSources>
            <apiSource>
	            <springmvc>true</springmvc>
                <locations>
                    <location>com.wordnik.swagger.sample</location>
                </locations>
                <schemes>
                    <scheme>http</scheme>
                    <scheme>https</scheme>
                </schemes>
                <host>www.example.com:8080</host>
                <basePath>/api</basePath>
                <info>
                    <title>Swagger Maven Plugin Sample</title>
                    <version>v1</version>
                    <!-- 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,
                     and note escape your description for xml -->
                    <description>
                        This is a sample.
                    </description>
                    <termsOfService>
                        http://www.github.com/kongchen/swagger-maven-plugin
                    </termsOfService>
                    <contact>
                        <email>[email protected]</email>
                        <name>Kong Chen</name>
                        <url>http://kongch.com</url>
                    </contact>
                    <license>
                        <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
                        <name>Apache 2.0</name>
                    </license>
                </info>
                <securityDefinitions>
                    <securityDefinition>
                        <name>basicAuth</name>
                        <type>basic</type>
                    </securityDefinition>
                    <securityDefinition>
                        <json>/securityDefinition.json</json>
                    </securityDefinition>
                </securityDefinitions>
                <responseMessageOverrides>
                    <responseMessageOverride>
                        <code>401</code>
                        <message>Unauthenticated - could not authenticate the user.</message>
                    </responseMessageOverride>
                    <responseMessageOverride>
                        <code>403</code>
                        <message>Forbidden - the user does not have the required permissions to execute this request.</message>
                    </responseMessageOverride>
                </responseMessageOverrides>

                <!-- Support classpath or file absolute path here.
                1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
                2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
                    "${basedir}/src/main/resources/template/hello.html" -->
                <templatePath>${basedir}/src/test/resources/strapdown.html.hbs</templatePath>
                <outputPath>${basedir}/generated/document.html</outputPath>
                <swaggerDirectory>${basedir}/generated/swagger-ui</swaggerDirectory>
                <swaggerApiReader>com.wordnik.swagger.jaxrs.reader.DefaultJaxrsApiReader</swaggerApiReader>
                <attachSwaggerArtifact>true</attachSwaggerArtifact>
                <modelConverters>io.swagger.validator.BeanValidator</modelConverters>
                <swaggerExtensions>
                    <swaggerExtension>com.example.VendorExtension</swaggerExtension>
                </swaggerExtensions>
                <enabledObjectMapperFeatures>
                    <feature>com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING</feature>
                    <feature>com.fasterxml.jackson.core.JsonParser$Feature.ALLOW_NUMERIC_LEADING_ZEROS</feature>
                </enabledObjectMapperFeatures>
                <disabledObjectMapperFeatures>
                    <feature>com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS</feature>
                </disabledObjectMapperFeatures>
                <operationIdFormat>{{className}}_{{methodName}}_{{httpMethod}}</operationIdFormat>
                <externalDocs>
                    <description>Example external docs</description>
                    <url>https://example.com/docs</url>
                </externalDocs>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <!-- Adding dependency to swagger-hibernate-validations to enable the BeanValidator as a custom
             model converter -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-hibernate-validations</artifactId>
            <version>1.5.6</version>
        </dependency>
    </dependencies>
</plugin>
...
</plugins>
</build>
</project>

FAQ

1. SNAPSHOT Version

SNAPSHOT versions are available for verifying issues and new features. If you would like to try to verify the fixed issues or the new added features, you may need to add a pluginRepository node in your pom.xml:

<pluginRepositories>
  <pluginRepository>
    <id>sonatype-snapshot</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    <releases>
      <enabled>false</enabled>
    </releases>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
  </pluginRepository>
</pluginRepositories>

2. Dependency conflicts

If you have package dependency conflict issues, such as jackson, joda-time, or jsr311-api. Run

mvn dependency:tree

to check which package introduces the one conflicts with yours, and then you can use <exclusion> configuration in pom.xml to exclude it.

Here's an example:

To exclude jakarta.ws.rs:jsr311-api:jar:1.1.1:compile from swagger-jaxrs_2.10:

<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-jaxrs_2.10</artifactId>
    <version>1.3.2</version>
    <exclusions>
        <exclusion>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. Building from source

To build from source and run tests, you should:

mvn install

Versions

Version
3.1.11
3.1.10
3.1.9