Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger Gradle Plugin not properly generating openapi.json file for spring mvc during build

I would like to import API definition files created with the swagger gradle plugin. I have a spring boot mvc app. I don't seem to be able to get the resolve settings correct to generate the openAPI.json. The gradle resolve task creates an empty file with just the "openapi" : "3.0.1" tag and value. The resolve task is not picking up the swagger and mvc api annotations. Can you point me in the right direction to find the right configuration settings for the swagger gradle resolve task?

The application displays the Spring-UI documentation without a problem when the app is running and I view the swagger-ui endpoint. The issue is during the gradle build there is a separate gradle task to generate the openAPI.json file that I would like to import into SwaggerHub as part of the application API catalog.

I have been unable to find any documentation anywhere on how to configure the swagger gradle plugin resolve task to pick up the Spring MVC API annotations. I can get it to work fine on another application that uses the jax-rs annotations so I am certain that it is a resolve task configuration issue.

A copy of my gradle.build file:

buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
        maven {
          url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE")

        // SwaggerHub Plugin Dependency
        classpath("gradle.plugin.io.swagger:swaggerhub:1.0.1")

        // Swagger Gradle Plugin Dependency
        classpath("io.swagger.core.v3:swagger-gradle-plugin:2.0.5") 

    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
// Required to upload swagger api doc to swaggerhub
apply plugin: 'io.swagger.swaggerhub'
// Required to generate swagger api doc file to upload to swaggerhub
apply plugin: 'io.swagger.core.v3.swagger-gradle-plugin'


jar {
    baseName = 'service-framework'
    version = '0.0.1-SNAPSHOT'
}

javadoc {
    source = sourceSets.main.allJava
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-web')

    // Swagger Dependencies to generate the api documentation
    compile('io.swagger:swagger-annotations:1.5.20')
    compile('io.springfox:springfox-swagger2:2.8.0')
    compile('io.springfox:springfox-swagger-ui:2.8.0')

    runtime('com.h2database:h2')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
    testCompile group: 'junit', name: 'junit-dep', version: '4.10'
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
}

// Command to the swagger gradle plugin to generate the api file for upload
// to swaggerhub
//
//
// Create the directory for output of the api document
File path = new File("${buildDir}/doc")
//
// The Swagger Core Task to create the json output file required to upload
// to SwaggerHub
//
resolve {
    outputFileName = 'openAPI'
    outputFormat = 'JSON'
    prettyPrint = 'TRUE'
    classpath = sourceSets.main.runtimeClasspath
    resourcePackages = ['test.serviceframework']
    outputPath = path.getAbsolutePath();
}

The resulting output in the openAPI.json file:

{
  "openapi" : "3.0.1"
}
like image 775
raptor21 Avatar asked Oct 18 '18 20:10

raptor21


People also ask

What is Gradle Swagger generator plugin?

Gradle Swagger Generator Plugin. This is a Gradle plugin for the following tasks: Validate an OpenAPI YAML. Generate source from an OpenAPI YAML using Swagger Codegen v2/v3 and OpenAPI Generator v3. Generate Swagger UI with an OpenAPI YAML. Generate ReDoc with an OpenAPI YAML.

What is Swagger Codegen and OpenAPI Generator?

Swagger Codegen and OpenAPI Generator enable you to generate REST clients quickly for your API with many languages and with the library of your choice. We can generate the client library using a CLI tool, Maven plugin, or Online API.

How to generate OpenAPI descriptions in Spring Boot using Maven?

Using springdoc-openapi Maven Plugin The springdoc-openapi library provides a Maven plugin springdoc-openapi-maven-plugin for generating OpenAPI descriptions in json and yaml formats. The springdoc-openapi-maven-plugin plugin works with the spring-boot-maven plugin. Maven runs the openapi plugin during the integration-test phase.

How to generate OpenAPI 3 specification docs with springdoc-OpenAPI?

To have springdoc-openapi automatically generate the OpenAPI 3 specification docs for our API, we simply add the springdoc-openapi-ui dependency to our pom.xml: Then when we run our application, the OpenAPI descriptions will be available at the path /v3/api-docs by default: To use a custom path, we can indicate in the application.properties file:


1 Answers

I know this is an old question but since the original post, the Swagger gradle plugin hasn't resoved this issue. However, Spring Doc has.

https://github.com/springdoc/springdoc-openapi-gradle-plugin

like image 200
Slihp Avatar answered Oct 13 '22 15:10

Slihp