Skip to content
Open
190 changes: 110 additions & 80 deletions modules/openapi-generator-gradle-plugin/README.adoc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ apply plugin: 'org.openapi.generator'
openApiMeta {
generatorName = "Sample"
packageName = "org.openapitools.example"
outputFolder = layout.buildDirectory.dir("meta").get().asFile.toString()
outputFolder = layout.buildDirectory.dir("meta")
}

openApiValidate {
inputSpec = "$rootDir/petstore-v3.0-invalid.yaml".toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0-invalid.yaml")
recommend = true
}

// Builds a Kotlin client by default.
openApiGenerate {
generatorName = "kotlin"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
outputDir = layout.buildDirectory.dir("kotlin").get().asFile.toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
outputDir = layout.buildDirectory.dir("kotlin")
apiPackage = "org.openapitools.example.api"
invokerPackage = "org.openapitools.example.invoker"
modelPackage = "org.openapitools.example.model"
Expand All @@ -54,11 +54,11 @@ openApiGenerate {
enablePostProcessFile = false
}

task buildJavaResttemplateSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
tasks.register('buildJavaResttemplateSdk', org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "java"
library = "resttemplate"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
outputDir = layout.buildDirectory.dir("java-resttemplate-api-client").get().asFile.toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
outputDir = layout.buildDirectory.dir("java-resttemplate-api-client")
apiPackage = "com.example.client"
invokerPackage = "com.example.invoker"
modelPackage = "com.example.cdm"
Expand All @@ -73,51 +73,51 @@ task buildJavaResttemplateSdk(type: org.openapitools.generator.gradle.plugin.tas
enablePostProcessFile = false
}

task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
tasks.register('buildGoSdk', org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "go"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
additionalProperties = [
packageName: "petstore"
]
outputDir = layout.buildDirectory.dir("go").get().asFile.toString()
outputDir = layout.buildDirectory.dir("go")
configOptions = [
dateLibrary: "threetenp"
]
}

task buildDotnetSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
tasks.register('buildDotnetSdk', org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
generatorName = "csharp"
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
additionalProperties = [
packageGuid: "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}",
packageGuid : "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}",
useCompareNetObjects: "true"
]
outputDir = layout.buildDirectory.dir("csharp").get().asFile.toString()
outputDir = layout.buildDirectory.dir("csharp")
globalProperties = [
models: "",
apis : "",
]
}

task generateGoWithInvalidSpec(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
tasks.register('generateGoWithInvalidSpec', org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
validateSpec = true
generatorName = "go"
inputSpec = "$rootDir/petstore-v3.0-invalid.yaml".toString()
inputSpec = layout.projectDirectory.file("petstore-v3.0-invalid.yaml")
additionalProperties = [
packageName: "petstore"
]
outputDir = layout.buildDirectory.dir("go").get().asFile.toString()
outputDir = layout.buildDirectory.dir("go")
configOptions = [
dateLibrary: "threetenp"
]
}

task validateGoodSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
def validateGoodSpec = tasks.register('validateGoodSpec', org.openapitools.generator.gradle.plugin.tasks.ValidateTask) {
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
}

task validateBadSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
inputSpec = "$rootDir/petstore-v3.0-invalid.yaml".toString()
def validateBadSpec = tasks.register('validateBadSpec', org.openapitools.generator.gradle.plugin.tasks.ValidateTask) {
inputSpec = layout.projectDirectory.file("petstore-v3.0-invalid.yaml")
}

task validateSpecs(dependsOn: ['validateGoodSpec', 'validateBadSpec'])
tasks.register('validateSpecs') { dependsOn validateGoodSpec, validateBadSpec }
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
project
)

generate.outputDir.set(project.layout.buildDirectory.dir("generate-resources/main").map { it.asFile.path })
generate.outputDir.convention(layout.buildDirectory.dir("generate-resources/main"))

tasks.apply {
register("openApiGenerators", GeneratorsTask::class.java).configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
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.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.property
import org.openapitools.generator.gradle.plugin.utils.isRemoteUri

/**
* Gradle project level extension object definition for the `generate` task
*
* @author Jim Schubert
*/
open class OpenApiGeneratorGenerateExtension(project: Project) {
open class OpenApiGeneratorGenerateExtension(private val project: Project) {
/**
* The verbosity of generation
*/
Expand All @@ -47,7 +48,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
/**
* The output target directory into which code will be generated.
*/
val outputDir = project.objects.property<String>()
val outputDir: DirectoryProperty = project.objects.directoryProperty()

/**
* The Open API 2.0/3.x specification location.
Expand All @@ -56,15 +57,15 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
* changes to any $ref referenced files. Use the `inputSpecRootDirectory` property to have Gradle track changes to
* an entire directory of spec files.
*/
val inputSpec = project.objects.property<String>()
val inputSpec: RegularFileProperty = project.objects.fileProperty()

/**
* Local root folder with spec files.
*
* By default, a merged spec file will be generated based on the contents of the directory. To disable this, set the
* `inputSpecRootDirectorySkipMerge` property.
*/
val inputSpecRootDirectory = project.objects.property<String>()
val inputSpecRootDirectory: DirectoryProperty = project.objects.directoryProperty()

/**
* Skip bundling all spec files into a merged spec file, if true.
Expand All @@ -81,7 +82,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
/**
* The template directory holding a custom template.
*/
val templateDir = project.objects.property<String>()
val templateDir: DirectoryProperty = project.objects.directoryProperty()

/**
* The template location (which may be a directory or a classpath location) holding custom templates.
Expand All @@ -104,7 +105,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
* File content should be in a json format { "optionKey":"optionValue", "optionKey1":"optionValue1"...}
* Supported options can be different for each language. Run config-help -g {generator name} command for language specific config options.
*/
val configFile = project.objects.property<String>()
val configFile: RegularFileProperty = project.objects.fileProperty()

/**
* Specifies if the existing files should be overwritten during the generation.
Expand Down Expand Up @@ -167,7 +168,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
val languageSpecificPrimitives = project.objects.listProperty<String>()

/**
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
* Specifies .openapi-generator-ignore list in the form of relative/path/to/file1,relative/path/to/file2. For example: README.md,pom.xml.
*/
val openapiGeneratorIgnoreList = project.objects.listProperty<String>()

Expand Down Expand Up @@ -279,7 +280,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
/**
* Specifies an override location for the .openapi-generator-ignore file. Most useful on initial generation.
*/
val ignoreFileOverride = project.objects.property<String>()
val ignoreFileOverride: RegularFileProperty = project.objects.fileProperty()

/**
* Remove prefix of operationId, e.g. config_getId => getId
Expand Down Expand Up @@ -413,22 +414,62 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {

@Suppress("MemberVisibilityCanBePrivate")
fun applyDefaults() {
releaseNote.set("Minor update")
inputSpecRootDirectorySkipMerge.set(false)
modelNamePrefix.set("")
modelNameSuffix.set("")
apiNameSuffix.set("")
generateModelTests.set(true)
generateModelDocumentation.set(true)
generateApiTests.set(true)
generateApiDocumentation.set(true)
configOptions.set(mapOf())
validateSpec.set(true)
logToStderr.set(false)
enablePostProcessFile.set(false)
skipValidateSpec.set(false)
generateAliasAsModel.set(false)
cleanupOutput.set(false)
dryRun.set(false)
releaseNote.convention("Minor update")
inputSpecRootDirectorySkipMerge.convention(false)
modelNamePrefix.convention("")
modelNameSuffix.convention("")
apiNameSuffix.convention("")
generateModelTests.convention(true)
generateModelDocumentation.convention(true)
generateApiTests.convention(true)
generateApiDocumentation.convention(true)
configOptions.convention(mapOf())
validateSpec.convention(true)
logToStderr.convention(false)
enablePostProcessFile.convention(false)
skipValidateSpec.convention(false)
generateAliasAsModel.convention(false)
cleanupOutput.convention(false)
dryRun.convention(false)
}
}

// ========================================================================
// Backwards-compatibility bridge setters for Groovy/Kotlin DSL
// These allow users to continue assigning paths as standard strings.
// ========================================================================

/** Backwards-compatibility bridge for outputDir */
fun setOutputDir(path: String) {
outputDir.set(project.layout.projectDirectory.dir(path))
}

/** Backwards-compatibility bridge for inputSpec */
fun setInputSpec(path: String) {
if (path.isRemoteUri()) {
remoteInputSpec.set(path)
} else {
inputSpec.set(project.layout.projectDirectory.file(path))
}
}

/** Backwards-compatibility bridge for inputSpecRootDirectory */
fun setInputSpecRootDirectory(path: String) {
inputSpecRootDirectory.set(project.layout.projectDirectory.dir(path))
}

/** Backwards-compatibility bridge for templateDir */
fun setTemplateDir(path: String) {
templateDir.set(project.layout.projectDirectory.dir(path))
}

/** Backwards-compatibility bridge for configFile */
fun setConfigFile(path: String) {
configFile.set(project.layout.projectDirectory.file(path))
}

/** Backwards-compatibility bridge for ignoreFileOverride */
fun setIgnoreFileOverride(path: String) {
ignoreFileOverride.set(project.layout.projectDirectory.file(path))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.openapitools.generator.gradle.plugin.extensions

import org.gradle.api.Project
import org.gradle.api.provider.ListProperty
import org.gradle.kotlin.dsl.listProperty
import org.openapitools.codegen.meta.Stability

Expand All @@ -29,13 +30,26 @@ open class OpenApiGeneratorGeneratorsExtension(project: Project) {
/**
* A list of stability indexes to include (value: all,beta,stable,experimental,deprecated). Excludes deprecated by default.
*/
val include = project.objects.listProperty<String>()
val include: ListProperty<String> = project.objects.listProperty()

init {
applyDefaults()
}

@Suppress("MemberVisibilityCanBePrivate")
fun applyDefaults() =
include.set(Stability.values().map { it.value() }.filterNot { it == Stability.DEPRECATED.value() })
}
fun applyDefaults() {
include.convention(
Stability.values()
.map { it.value() }
.filterNot { it == Stability.DEPRECATED.value() }
)
}

// ========================================================================
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
// Allows users to continue assigning lists directly via `=`
// ========================================================================
fun setInclude(items: Iterable<String>) {
include.set(items)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,32 +17,46 @@
package org.openapitools.generator.gradle.plugin.extensions

import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.property

/**
* Gradle project level extension object definition for the meta-generator task
*
* @author Jim Schubert
*/
open class OpenApiGeneratorMetaExtension(project: Project) {
open class OpenApiGeneratorMetaExtension(private val project: Project) {
/**
* The human-readable generator name of the newly created template generator.
*/
val generatorName = project.objects.property<String>()
val generatorName: Property<String> = project.objects.property()

/**
* The packageName generatorName to put the main class into (defaults to org.openapitools.codegen)
*/
val packageName = project.objects.property<String>()
val packageName: Property<String> = project.objects.property()

/**
* Where to write the generated files (current dir by default).
*/
val outputFolder = project.objects.property<String>()
val outputFolder: DirectoryProperty = project.objects.directoryProperty()

init {
generatorName.set("default")
packageName.set("org.openapitools.codegen")
outputFolder.set("")
generatorName.convention("default")
packageName.convention("org.openapitools.codegen")

// Use the native layout project directory instead of an empty string
outputFolder.convention(project.layout.projectDirectory)
}

// ========================================================================
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
// Allows users to continue assigning paths as standard strings.
// ========================================================================

/** Backwards-compatibility bridge for outputFolder */
fun setOutputFolder(path: String) {
outputFolder.set(project.layout.projectDirectory.dir(path))
}
}
Loading
Loading