Skip to content

Commit ce4ed62

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

9 files changed

Lines changed: 469 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
8484
description = "Validates an Open API 2.0 or 3.x specification document."
8585

8686
inputSpec.set(validate.inputSpec)
87+
remoteInputSpec.set(validate.remoteInputSpec)
8788
recommend.set(validate.recommend)
8889
treatWarningsAsErrors.set(validate.treatWarningsAsErrors)
8990
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,22 @@ open class OpenApiGeneratorMetaExtension(private val project: Project) {
5959
fun setOutputFolder(path: String) {
6060
outputFolder.set(project.layout.projectDirectory.dir(path))
6161
}
62+
63+
// ========================================================================
64+
// Kotlin DSL extension function for property setter
65+
// Allows Kotlin DSL users to call .set(String) on the outputFolder property
66+
// ========================================================================
67+
68+
/**
69+
* Extension function to allow setting outputFolder with a String path in Kotlin DSL.
70+
* Example: outputFolder.set("$buildDir/generated")
71+
*/
72+
fun DirectoryProperty.set(path: String) {
73+
if (this === outputFolder) {
74+
setOutputFolder(path)
75+
} else {
76+
// Fallback for any other DirectoryProperty
77+
this.set(project.layout.projectDirectory.dir(path))
78+
}
79+
}
6280
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,22 @@ open class OpenApiGeneratorValidateExtension(private val project: Project) {
6161
inputSpec.set(project.layout.projectDirectory.file(path))
6262
}
6363
}
64+
65+
// ========================================================================
66+
// Kotlin DSL extension function for property setter
67+
// Allows Kotlin DSL users to call .set(String) on the inputSpec property
68+
// ========================================================================
69+
70+
/**
71+
* Extension function to allow setting inputSpec with a String path in Kotlin DSL.
72+
* Example: inputSpec.set("$rootDir/api.yaml")
73+
*/
74+
fun RegularFileProperty.set(path: String) {
75+
if (this === inputSpec) {
76+
setInputSpec(path)
77+
} else {
78+
// Fallback for any other RegularFileProperty
79+
this.set(project.layout.projectDirectory.file(path))
80+
}
81+
}
6482
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,4 +933,42 @@ abstract class GenerateTask : DefaultTask() {
933933
}
934934
})
935935
}
936+
937+
// ========================================================================
938+
// Kotlin DSL extension functions for property setters
939+
// These allow Kotlin DSL users to call .set(String) on file/directory properties
940+
// when configuring tasks directly (e.g., tasks.named<GenerateTask>("openApiGenerate") { ... })
941+
// ========================================================================
942+
943+
/**
944+
* Extension function to allow setting file properties with a String path in Kotlin DSL.
945+
* Example: inputSpec.set("$rootDir/api.yaml")
946+
*/
947+
fun RegularFileProperty.set(path: String) {
948+
when (this) {
949+
inputSpec -> {
950+
if (path.isRemoteUri()) {
951+
remoteInputSpec.set(path)
952+
} else {
953+
this.set(layout.projectDirectory.file(path))
954+
}
955+
}
956+
configFile, ignoreFileOverride -> {
957+
this.set(layout.projectDirectory.file(path))
958+
}
959+
else -> {
960+
// Fallback for any other RegularFileProperty
961+
this.set(layout.projectDirectory.file(path))
962+
}
963+
}
964+
}
965+
966+
/**
967+
* Extension function to allow setting directory properties with a String path in Kotlin DSL.
968+
* Example: outputDir.set("$buildDir/generated")
969+
*/
970+
fun DirectoryProperty.set(path: String) {
971+
// All directory properties use the same conversion logic
972+
this.set(layout.projectDirectory.dir(path))
973+
}
936974
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/MetaTask.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.samskivert.mustache.Mustache
2020
import org.gradle.api.DefaultTask
2121
import org.gradle.api.GradleException
2222
import org.gradle.api.file.DirectoryProperty
23+
import org.gradle.api.file.ProjectLayout
2324
import org.gradle.api.provider.Property
2425
import org.gradle.api.tasks.CacheableTask
2526
import org.gradle.api.tasks.Input
@@ -35,6 +36,7 @@ import org.openapitools.codegen.templating.TemplateManagerOptions
3536
import java.io.File
3637
import java.io.IOException
3738
import java.nio.charset.Charset
39+
import javax.inject.Inject
3840

3941
/**
4042
* A task which generates a new generator (meta). Useful for redistributable generator packages.
@@ -44,6 +46,9 @@ import java.nio.charset.Charset
4446
@CacheableTask
4547
abstract class MetaTask : DefaultTask() {
4648

49+
@get:Inject
50+
abstract val layout: ProjectLayout
51+
4752
@get:Input
4853
abstract val generatorName: Property<String>
4954

@@ -135,4 +140,19 @@ abstract class MetaTask : DefaultTask() {
135140
split(Regex("[^a-zA-Z0-9]")).joinToString(separator = "-") { it.toLowerCase() }
136141

137142
private fun dir(vararg parts: String): String = parts.joinToString(separator = File.separator)
143+
144+
// ========================================================================
145+
// Kotlin DSL extension function for property setter
146+
// Allows Kotlin DSL users to call .set(String) on the outputFolder property
147+
// when configuring tasks directly (e.g., tasks.named<MetaTask>("openApiMeta") { ... })
148+
// ========================================================================
149+
150+
/**
151+
* Extension function to allow setting outputFolder with a String path in Kotlin DSL.
152+
* Example: outputFolder.set("$buildDir/generated")
153+
*/
154+
fun DirectoryProperty.set(path: String) {
155+
// All directory properties use the same conversion logic
156+
this.set(layout.projectDirectory.dir(path))
157+
}
138158
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/ValidateTask.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,30 @@ abstract class ValidateTask : DefaultTask() {
150150
logger.debug("No error validations from swagger-parser or internal validations.")
151151
logger.lifecycle("Spec is valid.")
152152
}
153+
154+
// ========================================================================
155+
// Kotlin DSL extension function for property setter
156+
// Allows Kotlin DSL users to call .set(String) on the inputSpec property
157+
// when configuring tasks directly (e.g., tasks.named<ValidateTask>("openApiValidate") { ... })
158+
// ========================================================================
159+
160+
/**
161+
* Extension function to allow setting inputSpec with a String path in Kotlin DSL.
162+
* Example: inputSpec.set("$rootDir/api.yaml")
163+
*/
164+
fun RegularFileProperty.set(path: String) {
165+
when (this) {
166+
inputSpec -> {
167+
if (path.isRemoteUri()) {
168+
remoteInputSpec.set(path)
169+
} else {
170+
this.set(layout.projectDirectory.file(path))
171+
}
172+
}
173+
else -> {
174+
// Fallback for any other RegularFileProperty
175+
this.set(layout.projectDirectory.file(path))
176+
}
177+
}
178+
}
153179
}

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,145 @@ paths:
767767
val versionFile = File(temp, "build/generated-kotlin/.openapi-generator/VERSION")
768768
assertTrue(versionFile.exists(), "Generator should have produced output")
769769
}
770+
771+
@Test
772+
fun `openApiGenerate should support Kotlin DSL set String syntax for all file and directory properties`() {
773+
// Create a spec file and config file
774+
val specContent = """
775+
openapi: 3.0.0
776+
info:
777+
title: Test API
778+
version: 1.0.0
779+
paths:
780+
/test:
781+
get:
782+
responses:
783+
'200':
784+
description: Success
785+
""".trimIndent()
786+
File(temp, "spec.yaml").writeText(specContent)
787+
788+
val configContent = """
789+
{
790+
"dateLibrary": "java8"
791+
}
792+
""".trimIndent()
793+
File(temp, "config.json").writeText(configContent)
794+
795+
// Build script using Kotlin DSL with .set(String) syntax for multiple properties
796+
val buildContents = """
797+
plugins {
798+
id("org.openapi.generator")
799+
}
800+
801+
openApiGenerate {
802+
generatorName.set("kotlin")
803+
inputSpec.set("${'$'}projectDir/spec.yaml")
804+
outputDir.set("${'$'}buildDir/generated")
805+
configFile.set("${'$'}projectDir/config.json")
806+
apiPackage.set("org.openapitools.example.api")
807+
modelPackage.set("org.openapitools.example.model")
808+
}
809+
""".trimIndent()
810+
811+
File(temp, "build.gradle.kts").writeText(buildContents)
812+
813+
// Run the generator
814+
val result = GradleRunner.create()
815+
.withProjectDir(temp)
816+
.withArguments("openApiGenerate", "--stacktrace")
817+
.withPluginClasspath()
818+
.build()
819+
820+
// Verify the task succeeded
821+
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome)
822+
823+
// Verify output was generated
824+
val versionFile = File(temp, "build/generated/.openapi-generator/VERSION")
825+
assertTrue(versionFile.exists(), "Generator should have produced output")
826+
}
827+
828+
@Test
829+
fun `openApiGenerate should support remote input spec with Kotlin DSL set String syntax`() {
830+
// Build script using remote URL with .set(String) syntax
831+
val specUrl = "https://raw.githubusercontent.com/OpenAPITools/openapi-generator/b6b8c0db872fb4a418ae496e89c7e656e14be165/modules/openapi-generator-gradle-plugin/src/test/resources/specs/petstore-v3.0.yaml"
832+
val buildContents = """
833+
plugins {
834+
id("org.openapi.generator")
835+
}
836+
837+
openApiGenerate {
838+
generatorName.set("kotlin")
839+
inputSpec.set("$specUrl")
840+
outputDir.set("${'$'}buildDir/generated-remote")
841+
apiPackage.set("org.openapitools.example.api")
842+
modelPackage.set("org.openapitools.example.model")
843+
}
844+
""".trimIndent()
845+
846+
File(temp, "build.gradle.kts").writeText(buildContents)
847+
848+
// Run the generator
849+
val result = GradleRunner.create()
850+
.withProjectDir(temp)
851+
.withArguments("openApiGenerate", "--stacktrace")
852+
.withPluginClasspath()
853+
.build()
854+
855+
// Verify the task succeeded
856+
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome)
857+
858+
// Verify output was generated
859+
val versionFile = File(temp, "build/generated-remote/.openapi-generator/VERSION")
860+
assertTrue(versionFile.exists(), "Generator should have produced output from remote spec")
861+
}
862+
863+
@Test
864+
fun `openApiGenerate should support task-level configuration with Kotlin DSL set String syntax`() {
865+
// Create a spec file
866+
val specContent = """
867+
openapi: 3.0.0
868+
info:
869+
title: Test API
870+
version: 1.0.0
871+
paths:
872+
/test:
873+
get:
874+
responses:
875+
'200':
876+
description: Success
877+
""".trimIndent()
878+
File(temp, "spec.yaml").writeText(specContent)
879+
880+
// Build script using Kotlin DSL configuring task directly
881+
val buildContents = """
882+
plugins {
883+
id("org.openapi.generator")
884+
}
885+
886+
tasks.named<org.openapitools.generator.gradle.plugin.tasks.GenerateTask>("openApiGenerate") {
887+
generatorName.set("kotlin")
888+
inputSpec.set("${'$'}projectDir/spec.yaml")
889+
outputDir.set("${'$'}buildDir/task-configured")
890+
apiPackage.set("org.openapitools.example.api")
891+
modelPackage.set("org.openapitools.example.model")
892+
}
893+
""".trimIndent()
894+
895+
File(temp, "build.gradle.kts").writeText(buildContents)
896+
897+
// Run the generator
898+
val result = GradleRunner.create()
899+
.withProjectDir(temp)
900+
.withArguments("openApiGenerate", "--stacktrace")
901+
.withPluginClasspath()
902+
.build()
903+
904+
// Verify the task succeeded
905+
assertEquals(TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome)
906+
907+
// Verify output was generated
908+
val versionFile = File(temp, "build/task-configured/.openapi-generator/VERSION")
909+
assertTrue(versionFile.exists(), "Generator should have produced output with task-level configuration")
910+
}
770911
}

0 commit comments

Comments
 (0)