Skip to content

Commit 9398514

Browse files
committed
modernize Gradle task input handling to support remote URIs (fix regression)
1 parent 1bebf1d commit 9398514

5 files changed

Lines changed: 61 additions & 19 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.gradle.api.file.RegularFileProperty
2222
import org.gradle.kotlin.dsl.listProperty
2323
import org.gradle.kotlin.dsl.mapProperty
2424
import org.gradle.kotlin.dsl.property
25+
import org.openapitools.generator.gradle.plugin.utils.isRemoteUri
2526

2627
/**
2728
* Gradle project level extension object definition for the `generate` task
@@ -444,7 +445,11 @@ open class OpenApiGeneratorGenerateExtension(private val project: Project) {
444445

445446
/** Backwards-compatibility bridge for inputSpec */
446447
fun setInputSpec(path: String) {
447-
inputSpec.set(project.layout.projectDirectory.file(path))
448+
if (path.isRemoteUri()) {
449+
remoteInputSpec.set(path)
450+
} else {
451+
inputSpec.set(project.layout.projectDirectory.file(path))
452+
}
448453
}
449454

450455
/** Backwards-compatibility bridge for inputSpecRootDirectory */

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.gradle.api.Project
2020
import org.gradle.api.file.RegularFileProperty
2121
import org.gradle.api.provider.Property
2222
import org.gradle.kotlin.dsl.property
23+
import org.openapitools.generator.gradle.plugin.utils.isRemoteUri
2324

2425
/**
2526
* Gradle project level extension object definition for the generators task
@@ -32,6 +33,11 @@ open class OpenApiGeneratorValidateExtension(private val project: Project) {
3233
*/
3334
val inputSpec: RegularFileProperty = project.objects.fileProperty()
3435

36+
/**
37+
* The remote input specification to validate. Supports URLs/URIs.
38+
*/
39+
val remoteInputSpec: Property<String> = project.objects.property()
40+
3541
/**
3642
* Whether to offer recommendations related to the validated specification document.
3743
*/
@@ -49,6 +55,10 @@ open class OpenApiGeneratorValidateExtension(private val project: Project) {
4955

5056
/** Backwards-compatibility bridge for inputSpec */
5157
fun setInputSpec(path: String) {
52-
inputSpec.set(project.layout.projectDirectory.file(path))
58+
if (path.isRemoteUri()) {
59+
remoteInputSpec.set(path)
60+
} else {
61+
inputSpec.set(project.layout.projectDirectory.file(path))
62+
}
5363
}
5464
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import org.openapitools.codegen.DefaultGenerator
3737
import org.openapitools.codegen.config.CodegenConfigurator
3838
import org.openapitools.codegen.config.GlobalSettings
3939
import org.openapitools.codegen.config.MergedSpecBuilder
40+
import org.openapitools.generator.gradle.plugin.utils.isRemoteUri
4041
import javax.inject.Inject
4142

4243
// =========================================================================================
@@ -318,14 +319,16 @@ abstract class GenerateTask : DefaultTask() {
318319
abstract val outputDir: DirectoryProperty
319320

320321
@Suppress("unused")
321-
@set:Option(option = "input", description = "The input specification.")
322-
@get:Internal
323-
var input: String? = null
324-
set(value) {
325-
if (value != null) {
322+
@Option(option = "input", description = "The input specification (local path or URL/URI).")
323+
fun setInput(value: String) {
324+
if (value.isNotEmpty()) {
325+
if (value.isRemoteUri()) {
326+
remoteInputSpec.set(value)
327+
} else {
326328
inputSpec.set(layout.projectDirectory.file(value))
327329
}
328330
}
331+
}
329332

330333
/**
331334
* The Open API 2.0/3.x specification location.

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import org.gradle.api.tasks.*
2727
import org.gradle.api.tasks.options.Option
2828
import org.openapitools.codegen.validations.oas.OpenApiEvaluator
2929
import org.openapitools.codegen.validations.oas.RuleConfiguration
30+
import org.openapitools.generator.gradle.plugin.utils.isRemoteUri
3031
import javax.inject.Inject
31-
import kotlin.text.get
32-
import kotlin.text.set
3332

3433
/**
3534
* A generator which validates an Open API spec. This task outputs a list of validation issues and errors.
@@ -53,10 +52,15 @@ abstract class ValidateTask : DefaultTask() {
5352
@get:Inject
5453
abstract val layout: ProjectLayout
5554

55+
@get:Optional
5656
@get:InputFile
5757
@get:PathSensitive(PathSensitivity.RELATIVE)
5858
abstract val inputSpec: RegularFileProperty
5959

60+
@get:Optional
61+
@get:Input
62+
abstract val remoteInputSpec: Property<String>
63+
6064
@get:Optional
6165
@get:Input
6266
abstract val recommend: Property<Boolean>
@@ -70,29 +74,37 @@ abstract class ValidateTask : DefaultTask() {
7074
treatWarningsAsErrors.convention(false)
7175
}
7276

73-
@get:Internal
74-
@set:Option(option = "input", description = "The input specification.")
75-
var input: String? = null
76-
set(value) {
77-
if (value != null) {
77+
@Suppress("unused")
78+
@Option(option = "input", description = "The input specification (local path or URL).")
79+
fun setInput(value: String) {
80+
if (value.isNotEmpty()) {
81+
if (value.isRemoteUri()) {
82+
remoteInputSpec.set(value)
83+
} else {
7884
inputSpec.set(layout.projectDirectory.file(value))
7985
}
8086
}
87+
}
8188

8289
@TaskAction
8390
fun doWork() {
84-
// Evaluate inputs
85-
val specFile = inputSpec.get().asFile
86-
val specPath = specFile.absolutePath
91+
// Evaluate inputs - prefer remote if provided, fallback to local file
92+
val specLocation = remoteInputSpec.orNull ?: inputSpec.orNull?.asFile?.absolutePath
93+
94+
if (specLocation == null) {
95+
throw GradleException("You must configure either inputSpec or provide a valid remote input via --input")
96+
}
97+
8798
val recommendations = recommend.get()
8899
val failOnWarnings = treatWarningsAsErrors.get()
89100

90-
logger.lifecycle("Validating spec $specPath")
101+
logger.lifecycle("Validating spec $specLocation")
91102

92103
val options = ParseOptions()
93104
options.isResolve = true
94105

95-
val result = OpenAPIParser().readLocation(specPath, null, options)
106+
// Pass specLocation instead of specPath
107+
val result = OpenAPIParser().readLocation(specLocation, null, options)
96108
val messages = result.messages.toSet()
97109

98110
val ruleConfiguration = RuleConfiguration()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.openapitools.generator.gradle.plugin.utils
2+
3+
// Compile the regex once for performance
4+
private val remoteUriRegex = "^[a-zA-Z][a-zA-Z0-9+\\-.]+?:.*".toRegex()
5+
6+
/**
7+
* Determines if a string is a remote URI (e.g., http:, jar:, s3:)
8+
* while safely ignoring 1-letter Windows drives (e.g., C:\).
9+
*/
10+
internal fun String.isRemoteUri(): Boolean {
11+
return this.matches(remoteUriRegex)
12+
}

0 commit comments

Comments
 (0)