@@ -10,6 +10,7 @@ import java.nio.file.Files.createTempDirectory
1010import java.nio.file.Paths
1111import kotlin.test.assertEquals
1212import kotlin.test.assertFalse
13+ import kotlin.test.assertNotNull
1314import kotlin.test.assertTrue
1415
1516class GenerateTaskDslTest : TestBase () {
@@ -642,4 +643,87 @@ class GenerateTaskDslTest : TestBase() {
642643 " Expected a successful run, but found ${result.task(" :openApiGenerate" )?.outcome} "
643644 )
644645 }
646+
647+ @DataProvider(name = " gradle_version_provider" )
648+ private fun gradleVersionProvider (): Array <Array <String >> = arrayOf(
649+ arrayOf(" 8.14.4" , " STRING" ),
650+ arrayOf(" 8.14.4" , " FILE" ),
651+ arrayOf(" 8.5" , " STRING" ),
652+ arrayOf(" 8.5" , " FILE" ),
653+ )
654+
655+ @Test(dataProvider = " gradle_version_provider" )
656+ fun `test implicit task wiring from producer task to generator` (gradleVersion : String , format : String ) {
657+ val propertyFormat = PropertyFormat .valueOf(format)
658+
659+ // Build script with a producer task that creates a spec file at execution time
660+ val buildContents = """
661+ plugins {
662+ id 'org.openapi.generator'
663+ }
664+
665+ // A task that creates a spec file at execution time
666+ abstract class SpecProducerTask extends DefaultTask {
667+ @OutputFile
668+ abstract RegularFileProperty getOutputFile()
669+
670+ @TaskAction
671+ void create() {
672+ getOutputFile().get().asFile.text = '''
673+ openapi: 3.0.0
674+ info:
675+ title: Produced API
676+ version: 1.0.0
677+ paths:
678+ /pets:
679+ get:
680+ responses:
681+ '200':
682+ description: Success
683+ '''.stripIndent()
684+ }
685+ }
686+
687+ // Register the producer
688+ def producer = tasks.register("produceSpec", SpecProducerTask) {
689+ outputFile = layout.buildDirectory.file("dynamic-spec.yaml")
690+ }
691+
692+ // Configure the generator with implicit wiring
693+ openApiGenerate {
694+ generatorName = "kotlin"
695+ ${if (propertyFormat == PropertyFormat .FILE ) {
696+ " inputSpec.set(producer.flatMap { it.outputFile })"
697+ } else {
698+ " inputSpec = producer.flatMap { it.outputFile.get().asFile.absolutePath }"
699+ }}
700+ outputDir = layout.buildDirectory.dir("generated")
701+ apiPackage = "org.openapitools.example.api"
702+ modelPackage = "org.openapitools.example.model"
703+ }
704+ """ .trimIndent()
705+
706+ File (temp, " build.gradle" ).writeText(buildContents)
707+
708+ // Run the generator
709+ val result = GradleRunner .create()
710+ .withProjectDir(temp)
711+ .withArguments(" openApiGenerate" )
712+ .withPluginClasspath()
713+ .withGradleVersion(gradleVersion)
714+ .build()
715+
716+ // Verify: The producer task MUST have run because the generator needed its output
717+ val producerTask = result.task(" :produceSpec" )
718+ val generatorTask = result.task(" :openApiGenerate" )
719+
720+ assertNotNull(producerTask, " Producer task should have been part of the graph" )
721+ assertEquals(TaskOutcome .SUCCESS , producerTask.outcome, " Producer task should have succeeded" )
722+ assertNotNull(generatorTask, " Generator task should have been part of the graph" )
723+ assertEquals(TaskOutcome .SUCCESS , generatorTask.outcome, " Generator task should have succeeded" )
724+
725+ // Check that the generator actually produced something
726+ val versionFile = File (temp, " build/generated/.openapi-generator/VERSION" )
727+ assertTrue(versionFile.exists(), " Generator should have run and produced output" )
728+ }
645729}
0 commit comments