OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG] Gradle plugin: incremental build does not work if `outputDir` is not in `$buildDir` #11356

Open alafanechere opened 2 years ago

alafanechere commented 2 years ago

Bug Report Checklist

Description

When I use the GenerateTask and want to target an outputDir which path is not in the $buildDir the client I'm generating is generated on each build and I don't benefit from incremental builds (UP-TO-DATE flag on task). I'm wondering if this is something related to Gradle our this plugin specifically and if there's an existing workaround.

If I use the $buildDir in the outputDir path the incremental build works, and my client get generated only upon spec updates.

openapi-generator version

5.3.1

Steps to reproduce

No incremental builds with the following build.gradle

import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
    id "org.openapi.generator" version "5.3.1"
    id 'airbyte-python'
    id 'airbyte-docker'
}

airbytePython {
    moduleDirectory 'octavia_cli'
}

task generateApiClient(type: GenerateTask) {
    inputSpec =  "$rootDir.absolutePath/airbyte-api/src/main/openapi/config.yaml"
    outputDir = "$projectDir/airbyte_api_client"

    generatorName = "python"
    packageName = "airbyte_api_client"
}

Works as expected when using $buildDir in the outputDir path:

import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
    id "org.openapi.generator" version "5.3.1"
    id 'airbyte-python'
    id 'airbyte-docker'
}

airbytePython {
    moduleDirectory 'octavia_cli'
}

task generateApiClient(type: GenerateTask) { inputSpec = "$rootDir.absolutePath/airbyte-api/src/main/openapi/config.yaml" outputDir = "$buildDir/airbyte_api_client"

generatorName = "python"
packageName = "airbyte_api_client"

}

Related issues/PRs

@HenningWaack PR's https://github.com/OpenAPITools/openapi-generator/pull/6716 introduced support for incremental build.

nbrugger-tgm commented 2 years ago

Hi I dug a bit deeper as i am experiencing the same issue but in my case it was my own fault.

To investigate the bug please run you gradle command with --info it will show you why the task is re-run.

If you get an output message such as "From property outputDir file "xyz" was removed" you can use the following code to find whats going wrong

Set<File> bev;
tasks.openApiGenerate.doFirst {
    println "Before : " + it.outputs.files.asFileTree.files.size()
    bev = it.outputs.files.asFileTree.files
}
tasks.openApiGenerate.doLast { it ->
    println "After : " + it.outputs.files.asFileTree.files.size()
    var newOut = new HashSet<>(it.outputs.files.asFileTree.files);
    newOut.removeAll(bev);
    println "Diff (ADDED) : " + newOut
    bev.removeAll(it.outputs.files.asFileTree.files)
    println "Diff (REMOVED) : " + bev
}

This just compares the diff between the output dir before and after the task