swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

Gradle project static HTML doesn't get generated #6949

Open jsbournival opened 6 years ago

jsbournival commented 6 years ago
Description

Given a Gradle project with the following 2 tasks (one generating server code with language spring and the other one the static HTML with language html2). The problem is that nothing gets written in the case of the static HTML task when running the build task. If generate HTML task gets called explicitly it works fine.

Swagger-codegen version

io.swagger:swagger-annotations:1.5.16 io.swagger:swagger-codegen:2.2.3

Swagger declaration file content or url
def generateServerCodeTask(name, swaggerInputFile, swaggerOutputDir, apiPackage, modelPackage) {
  return tasks.create("${name}GenerateServerCode") {

    doFirst {
      io.swagger.parser.SwaggerParser parser = new io.swagger.parser.SwaggerParser()
      io.swagger.parser.util.SwaggerDeserializationResult result = parser.readWithInfo(swaggerInputFile, null, true)
      if (!result.messages.isEmpty()) {
        System.err.println("Swagger validation: " + result.messages.stream().collect(java.util.stream.Collectors.joining("\n")))
      }
    }

    doLast {
      def config = new io.swagger.codegen.config.CodegenConfigurator()
      config.inputSpec = swaggerInputFile
      config.outputDir = swaggerOutputDir
      config.lang = 'spring'
      config.templateDir = "${rootProject.rootDir}/swagger/templates/server/"
      config.apiPackage = apiPackage
      config.modelPackage = modelPackage
      config.modelNameSuffix = "WS"
      config.additionalProperties = [
        "dateLibrary"      : "java8",
        "interfaceOnly"    : true,
        "useBeanValidation": true
      ]
      config.skipOverwrite = false
      config.addSystemProperty("models", "")
      config.addSystemProperty("apis", "")
      config.addSystemProperty("apiTests", "false")
      config.addSystemProperty("apiDocs", "false")
      config.addSystemProperty("modelDocs", "false")
      config.addSystemProperty("modelTests", "false")

      new io.swagger.codegen.DefaultGenerator().opts(config.toClientOptInput()).generate()
    }
  }
}

def generateServerHtmlDocTask(name, swaggerInputFile, htmlDocOutputDir) {
  return tasks.create("${name}GenerateHtmlDoc") {

    doFirst {
      io.swagger.parser.SwaggerParser parser = new io.swagger.parser.SwaggerParser()
      io.swagger.parser.util.SwaggerDeserializationResult result = parser.readWithInfo(swaggerInputFile, null, true)
      if (!result.messages.isEmpty()) {
        System.err.println("Swagger validation: " + result.messages.stream().collect(java.util.stream.Collectors.joining("\n")))
      }
    }

    doLast {
      def config = new io.swagger.codegen.config.CodegenConfigurator()
      config.inputSpec = swaggerInputFile
      config.outputDir = htmlDocOutputDir
      config.lang = 'html2'

      new io.swagger.codegen.DefaultGenerator().opts(config.toClientOptInput()).generate()
    }
  }
}

Attaching both tasks to compileJava.

compileJava {
    dependsOn parent.generateServerHtmlDocTask(project.name, "${projectDir}/swagger/api-spec.yml", "${buildDir}/docs"), parent.generateServerCodeTask(project.name, "${projectDir}/swagger/api-spec.yml", "${buildDir}/swagger-code", "com.company.api", "com.company.api.dto")
}
Command line used for generation
./gradlew clean build
Steps to reproduce
  1. Setup a multi-module Gradle project with java and spring boot plugins
  2. include the 2 tasks and an arbitrary YAML input file
  3. run ./gradlew clean build
Suggest a fix/enhancement
wing328 commented 6 years ago

What if you replace html2 with other generator (e.g. ruby, php)?

wing328 commented 6 years ago

You may find gradle plug-in useful: https://github.com/swagger-api/swagger-codegen#gradle-integration

jsbournival commented 6 years ago

I tried with other generator, and they are working fine.

wing328 commented 6 years ago

@jsbournival I really have no clue. Maybe using the JAR (swagger-codegen-cli) to generate html2 as a workaround for the time being.