Reference external file (openapi spec) from gradle plugin (openapitools generator)

Viewed 3515

I am attempting to build an application that references an openapi spec that is already published in artifactory. That means I'll be pulling the foo.yaml in as a dependency, but I can't seem to figure out how to actually reference that file by the openapitools generator plugin.

Given that openapi specs can be used to generate both server code and client code, it makes perfect sense that it is published separately and simply pulled in and referenced by implementations.

com.company.bar-1.0.10 contains foo.yaml at the top level of the jar.

I've added the dependency at the top level of the build.gradle.kts file and I've also added it as a part of the plugin task itself.

task generateFooCode(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {

generatorName = "java"
apiPackage = 'com.ehi.gbo.openapiconnect.api.foo'
modelPackage = 'com.ehi.gbo.openapiconnect.model.foo'
invokerPackage = 'com.ehi.gbo.openapiconnect.common.invoker'
inputSpec = "foo.yaml".toString()
outputDir = "$buildDir/generated-sources/foo".toString()
configOptions = [
        dateLibrary          : "java8",
        useTags              : true,
        interfaceOnly        : true,
        delegatePattern      : false,
        useBeanValidation    : false,
        performBeanValidation: false,
        useOptional          : false,
        serviceImplementation: false,
        serviceInterface     : false,
        java8                : false,
        serializableModel    : true,
        skipDefaultInterface : true,
        reactive             : false,
]
configurations {
    dependencies {
        implementation 'com.company.bar:foo-api:1.0.10'
    }
}

}

Results I'm getting: * What went wrong: Execution failed for task ':generateFooCode'.

There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI). | Error count: 1, Warning count: 0 Errors: -unable to read location foo.yaml

1 Answers

After a lot of googling, I came across a very elegant solution.

configurations {
    api
}
  dependencies {
    api 'somegroup:someArtifact:someVersion'
}
  task extractApi(type: Sync) {
    dependsOn configurations.api

    from { // use of closure defers evaluation until execution time
        configurations.api.collect { zipTree(it) }
    }
    into "$buildDir/api/"
}

Then I could just make the inputSpec reference $buildDir/api/spec.yaml

Related