Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[suggestion] Gradle plugin input file instead of input string #18573

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,20 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
description = "Validates an Open API 2.0 or 3.x specification document."

inputSpec.set(validate.inputSpec)
inputFile.set(validate.inputFile)
recommend.set(validate.recommend)
}

register("openApiGenerate", GenerateTask::class.java).configure {
group = pluginGroup
description =
"Generate code via Open API Tools Generator for Open API 2.0 or 3.x specification documents."
description = "Generate code via Open API Tools Generator for Open API 2.0 or 3.x specification documents."

verbose.set(generate.verbose)
validateSpec.set(generate.validateSpec)
generatorName.set(generate.generatorName)
outputDir.set(generate.outputDir)
inputSpec.set(generate.inputSpec)
inputFile.set(generate.inputFile)
inputSpecRootDirectory.set(generate.inputSpecRootDirectory)
remoteInputSpec.set(generate.remoteInputSpec)
templateDir.set(generate.templateDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package org.openapitools.generator.gradle.plugin.extensions

import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.property
import java.io.File

/**
* Gradle project level extension object definition for the `generate` task
Expand Down Expand Up @@ -52,8 +51,14 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
/**
* The Open API 2.0/3.x specification location.
*/
@Deprecated("Input of type String is deprecated.", ReplaceWith("inputFile"))
val inputSpec = project.objects.property<String>()

/**
* The Open API 2.0/3.x specification location.
*/
val inputFile = project.objects.property<File>().convention(inputSpec.map { File(it) })

/**
* Local root folder with spec files
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.openapitools.generator.gradle.plugin.extensions

import org.gradle.api.Project
import org.gradle.kotlin.dsl.property
import java.io.File

/**
* Gradle project level extension object definition for the generators task
Expand All @@ -28,8 +29,14 @@ open class OpenApiGeneratorValidateExtension(project: Project) {
/**
* The input specification to validate. Supports all formats supported by the Parser.
*/
@Deprecated("Input of type String is deprecated.", ReplaceWith("inputFile"))
val inputSpec = project.objects.property<String>()

/**
* The input specification to validate. Supports all formats supported by the Parser.
*/
val inputFile = project.objects.property<File>().convention(inputSpec.map { File(it) })

/**
* Whether to offer recommendations related to the validated specification document.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.kotlin.dsl.listProperty
Expand All @@ -45,6 +43,7 @@ import org.openapitools.codegen.DefaultGenerator
import org.openapitools.codegen.config.CodegenConfigurator
import org.openapitools.codegen.config.GlobalSettings
import org.openapitools.codegen.config.MergedSpecBuilder
import java.io.File

/**
* A task which generates the desired code.
Expand Down Expand Up @@ -87,22 +86,23 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
@get:OutputDirectory
val outputDir = project.objects.property<String>()

@Suppress("unused")
@set:Option(option = "input", description = "The input specification.")
@Internal
var input: String? = null
set(value) {
inputSpec.set(value)
}

/**
* The Open API 2.0/3.x specification location.
*/
@Optional
@get:InputFile
@PathSensitive(PathSensitivity.RELATIVE)
@Deprecated("Input of type String is deprecated.", ReplaceWith("inputFile"))
val inputSpec = project.objects.property<String>()

/**
* The Open API 2.0/3.x specification location.
*/
@Optional
@get:InputFile
@PathSensitive(PathSensitivity.NONE)
val inputFile = project.objects.property<File>().convention(inputSpec.map { File(it) })

/**
* Local root folder with spec files
*/
Expand Down Expand Up @@ -608,8 +608,8 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
fun doWork() {
var resolvedInputSpec = ""

inputSpec.ifNotEmpty { value ->
resolvedInputSpec = value
inputFile.ifNotEmpty { value ->
resolvedInputSpec = value.absolutePath
}

remoteInputSpec.ifNotEmpty { value ->
Expand Down Expand Up @@ -684,8 +684,8 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
GlobalSettings.setProperty(CodegenConstants.WITH_XML, withXml.get().toString())
}

if (inputSpec.isPresent && remoteInputSpec.isPresent) {
logger.warn("Both inputSpec and remoteInputSpec is specified. The remoteInputSpec will take priority over inputSpec.")
if (inputFile.isPresent && remoteInputSpec.isPresent) {
logger.warn("Both inputFile and remoteInputSpec is specified. The remoteInputSpec will take priority over inputSpec.")
}

configurator.setInputSpec(resolvedInputSpec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ import org.gradle.api.GradleException
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.internal.logging.text.StyledTextOutput
import org.gradle.internal.logging.text.StyledTextOutputFactory
import org.gradle.kotlin.dsl.property
import org.openapitools.codegen.validations.oas.OpenApiEvaluator
import org.openapitools.codegen.validations.oas.RuleConfiguration
import java.io.File

/**
* A generator which validates an Open API spec. This task outputs a list of validation issues and errors.
Expand All @@ -52,34 +51,33 @@ import org.openapitools.codegen.validations.oas.RuleConfiguration
* @author Jim Schubert
*/
open class ValidateTask : DefaultTask() {

@get:InputFile
@Optional
@Deprecated("Input of type String is deprecated.", ReplaceWith("inputFile"))
@PathSensitive(PathSensitivity.RELATIVE)
val inputSpec = project.objects.property<String>()
val inputSpec = project.objects.property<String>().convention(null)

@get:InputFile
val inputFile = project.objects.property<File>().convention(inputSpec.map { File(it) })

@Optional
@Input
val recommend = project.objects.property<Boolean>().convention(true)

@get:Internal
@set:Option(option = "input", description = "The input specification.")
var input: String? = null
set(value) {
inputSpec.set(value)
}

@TaskAction
fun doWork() {
val logger = Logging.getLogger(javaClass)

val spec = inputSpec.get()
val spec = inputFile.get()
val recommendations = recommend.get()

logger.quiet("Validating spec $spec")

val options = ParseOptions()
options.isResolve = true

val result = OpenAPIParser().readLocation(spec, null, options)
val result = OpenAPIParser().readContents(spec.readText(), null, options)
val messages = result.messages.toSet()
val out = services.get(StyledTextOutputFactory::class.java).create("openapi")

Expand Down