GNG
GNG

Reputation: 1531

Including info object in swagger maven plugin configuration

I am having trouble including an info object in my swagger output json file. I am using the swagger-maven-plugin from https://github.com/swagger-api/swagger-core. Here is what I have tried...

  1. I have tried including an info object in my pom.xml like this...

            <plugin>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.0.9</version>
            <configuration>
                <outputFileName>openapi</outputFileName>
                <outputPath>${project.build.directory}/openapi-json</outputPath>
                <outputFormat>JSONANDYAML</outputFormat>
                <resourcePackages>
                    <package>packageName</package>
                </resourcePackages>
                <info>
                    <version>
                        1.0
                    </version>
                    <title>
                        Swagger Pet Sample App Config File
                    </title>
                    <description>
                    This is a sample server Petstore server.  You can find out more about Swagger.                          
                    </description>
                    <termsOfService>http://swagger.io/terms/
                    </termsOfService>
                    <license>
                        <name>
                            Apache2.0
                        </name>
                        <url>
                            http://www.apache.org/licenses/LICENSE-2.0.html
                        </url>
                    </license>  
                    <contact>
                        <email>
                            [email protected]
                        </email>
                    </contact>
                </info>
                <prettyPrint>TRUE</prettyPrint>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    

I have also tried adding an openapi-configuration.yaml file in my path. The file looks like this. I copied this file from the plugin repo Readme page, so the contents are different than in my first approach above.

    resourcePackages:
- packageName
prettyPrint: true
cacheTTL: 0
openAPI:
  info:
    version: '1.0'
    title: Swagger Pet Sample App Config File
    description: 'This is a sample server Petstore server.  You can find out more
      about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net,
      #swagger](http://swagger.io/irc/).  For this sample, you can use the api key
      `special-key` to test the authorizat ion filters.'
    termsOfService: http://swagger.io/terms/
    contact:
      email: [email protected]
    license:
      name: Apache 2.0
      url: http://www.apache.org/licenses/LICENSE-2.0.html

Neither of these approaches work.

What am I missing? Cheers.

Upvotes: 2

Views: 8913

Answers (3)

fguyot
fguyot

Reputation: 21

In your JAX-RS Application Class, use the @OpenAPIDefinition annotation to define your swagger information following the schema of the OpenAPI Specification:

package test.webapp.rest.application;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
    
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.annotations.info.Info;
@ApplicationPath("/rest/*")
@OpenAPIDefinition(
            info = @Info(title="This is my title", 
            description="This is my description", version="9.9.9"), 
            servers = @Server(url="http://localhost:8080/test-webapp-rest/rest"))
public class RESTApplication extends Application{
    ...
}

In your pom.xml, add the package of this Application Class to the "resourcePackages" of the swagger-maven-plugin:

        <!-- GENERATE openapi.json in /src/main/webapp/swagger-ui-->
        <plugin>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.1.5</version>
            <configuration>
                <outputFileName>openapi</outputFileName>
                <outputPath>${basedir}/src/main/webapp/swagger-ui</outputPath>
                <outputFormat>JSONANDYAML</outputFormat>
                <resourcePackages>
                    <package>test.webapp.rest.application</package>
                    <package>test.webapp.rest.resource</package>
                </resourcePackages>
                <prettyPrint>TRUE</prettyPrint>
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This second step is the essential step to have your information generated in your JSON or YAML:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "This is my title",
    "description" : "This is my description",
    "version" : "9.9.9"
  },
  "servers" : [ {
    "url" : "http://localhost:8080/test-webapp-rest/rest",
    "variables" : { }
  } ],
  ...
}


openapi: 3.0.1
info:
  title: This is my title
  description: This is my description
  version: 9.9.9
servers:
- url: http://localhost:8080/test-webapp-rest/rest
  variables: {}

Upvotes: 2

Praveen Jayapal
Praveen Jayapal

Reputation: 73

The "info" tag should comes under "apiSource" tag like below

<configuration>
    <apiSources>
        <apiSource>
            <springmvc>true</springmvc>
            <locations>com.xx.yyy.oooo</locations>
            <schemes>http,https</schemes>
            <host>@YYYY@</host>
            <basePath>@XXXX@</basePath>
            <info>

            </info>

Upvotes: -1

GNG
GNG

Reputation: 1531


Update I got this working as follows... In my pom.xml...

            <plugin>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-maven-plugin</artifactId>
            <version>2.0.9</version>                
            <configuration>
                <outputFileName>openapi</outputFileName>
                <outputPath>${project.build.directory}/openapi-json</outputPath>
                <outputFormat>JSONANDYAML</outputFormat>
                <resourcePackages>
                    <package>packageName.services</package>
                </resourcePackages>
                <configurationFilePath>${project.basedir}/openapi.yaml</configurationFilePath>              
            </configuration>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>resolve</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And then in a separate configuration YAML file...

  openAPI:
  info:
    version: '1.0'
    title: API Documentation
    description: 'This is documentation for the Foosite API. You can find out more about FooSite at FooSite.org.'
    termsOfService: http://foosite.org/terms/
    license:
      name: Apache2.0
      url: http://www.apache.org/licenses/LICENSE-2.0.html
    contact:
      email: [email protected]
prettyPrint: true

Upvotes: 4

Related Questions