Skip to content

Commit a9747b3

Browse files
committed
fallback for string file path in kotlin syntax
1 parent 26bb435 commit a9747b3

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,42 @@ open class OpenApiGeneratorGenerateExtension(private val project: Project) {
472472
ignoreFileOverride.set(project.layout.projectDirectory.file(path))
473473
}
474474

475+
// ========================================================================
476+
// Kotlin DSL extension functions for property setters
477+
// These allow Kotlin DSL users to call .set(String) on file/directory properties
478+
// ========================================================================
479+
480+
/**
481+
* Extension function to allow setting inputSpec with a String path in Kotlin DSL.
482+
* Example: inputSpec.set("$rootDir/api.yaml")
483+
*/
484+
fun RegularFileProperty.set(path: String) {
485+
if (this === inputSpec) {
486+
setInputSpec(path)
487+
} else if (this === configFile) {
488+
setConfigFile(path)
489+
} else if (this === ignoreFileOverride) {
490+
setIgnoreFileOverride(path)
491+
} else {
492+
// Fallback for any other RegularFileProperty
493+
this.set(project.layout.projectDirectory.file(path))
494+
}
495+
}
496+
497+
/**
498+
* Extension function to allow setting directory properties with a String path in Kotlin DSL.
499+
* Example: outputDir.set("$buildDir/generated")
500+
*/
501+
fun DirectoryProperty.set(path: String) {
502+
if (this === outputDir) {
503+
setOutputDir(path)
504+
} else if (this === inputSpecRootDirectory) {
505+
setInputSpecRootDirectory(path)
506+
} else if (this === templateDir) {
507+
setTemplateDir(path)
508+
} else {
509+
// Fallback for any other DirectoryProperty
510+
this.set(project.layout.projectDirectory.dir(path))
511+
}
512+
}
475513
}

modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,53 @@ paths:
718718
val versionFile = File(temp, "build/generated/.openapi-generator/VERSION")
719719
assertTrue(versionFile.exists(), "Generator should have run and produced output")
720720
}
721+
722+
@Test
723+
fun `openApiGenerate should support Kotlin DSL set String syntax`() {
724+
// Create a spec file
725+
val specContent = """
726+
openapi: 3.0.0
727+
info:
728+
title: Test API
729+
version: 1.0.0
730+
paths:
731+
/test:
732+
get:
733+
responses:
734+
'200':
735+
description: Success
736+
""".trimIndent()
737+
File(temp, "spec.yaml").writeText(specContent)
738+
739+
// Build script using Kotlin DSL with .set(String) syntax
740+
val buildContents = """
741+
plugins {
742+
id("org.openapi.generator")
743+
}
744+
745+
openApiGenerate {
746+
generatorName.set("kotlin")
747+
inputSpec.set("${'$'}projectDir/spec.yaml")
748+
outputDir.set("${'$'}buildDir/generated-kotlin")
749+
apiPackage.set("org.openapitools.example.api")
750+
modelPackage.set("org.openapitools.example.model")
751+
}
752+
""".trimIndent()
753+
754+
File(temp, "build.gradle.kts").writeText(buildContents)
755+
756+
// Run the generator
757+
val result = GradleRunner.create()
758+
.withProjectDir(temp)
759+
.withArguments("openApiGenerate", "--stacktrace")
760+
.withPluginClasspath()
761+
.build()
762+
763+
// Verify the task succeeded
764+
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome)
765+
766+
// Verify output was generated
767+
val versionFile = File(temp, "build/generated-kotlin/.openapi-generator/VERSION")
768+
assertTrue(versionFile.exists(), "Generator should have produced output")
769+
}
721770
}

0 commit comments

Comments
 (0)