Search code examples
kotlingradlegradle-kotlin-dslopenapi-generator

How to generate openapi client from uri in Gradle


I'm probably trying to do something strange, since this doesn't seem like a common question (or maybe I'm asking it all wrong). I was expecting this to be straightforward.

Basically, what I am looking for is a way to do the same as the following, except by using the gradle openapi-generator plugin: openapi-generator generate -i www.example.com/openapi-doc -g spring

What I have tried is the following (and the associated errors):

  1. inputSpec.set("www.example.com/openapi-doc") --> Cannot convert URL {} to a file
  2. inputSpec.set(URL("www.example.com/openapi-doc").readText()) --> specified for property 'inputSpec' does not exist

The actual code looks something like this:

tasks.register<GenerateTask>("generateClient") {
    validateSpec
    generatorName.set("spring")
    library.set("spring-cloud")
//    inputSpec.set("$openapiSpecDir/client/openapi.json") <-- *I am currently using a file, which I don't want to do*
    inputSpec.set("https://www.example.com/openapi-doc")
    outputDir.set(generatedClientDir)
    apiPackage.set("org.example.api")
    modelPackage.set("org.example.model")
    skipOverwrite.set(false)
    templateDir.set("$rootDir/src/main/resources/openapi/templates/client")
    configOptions.put("java8", "false")
    configOptions.put("serializationLibrary", "jackson")
    configOptions.put("dateLibrary", "java8")
}

Solution

  • Assuming you're using the OpenAPI Generator Gradle Plugin, at the time of writing this answer, getting the inputSpec from a URL is not supported. However, for Maven this has been implemented (Issue #2241 closed with PR #3826), so chances are good to have it implemented with a feature request that gets the Gradle plugin on par with its Maven counterpart.

    That being said, you may want to look into Gradle Download Task. Gradle Download Task is a plugin that let's you download files from a URL. The downloaded file can be used to feed it into the OpenAPI generator.