Skip to content

Commit 5a2ba7c

Browse files
committed
migrate to modern api. Add bridge methods and tests
1 parent e01fdee commit 5a2ba7c

17 files changed

Lines changed: 894 additions & 815 deletions

modules/openapi-generator-gradle-plugin/samples/local-spec/build.gradle

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ apply plugin: 'org.openapi.generator'
2323
openApiMeta {
2424
generatorName = "Sample"
2525
packageName = "org.openapitools.example"
26-
outputFolder = layout.buildDirectory.dir("meta").get().asFile.toString()
26+
outputFolder = layout.buildDirectory.dir("meta")
2727
}
2828

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

3434
// Builds a Kotlin client by default.
3535
openApiGenerate {
3636
generatorName = "kotlin"
37-
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
38-
outputDir = layout.buildDirectory.dir("kotlin").get().asFile.toString()
37+
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
38+
outputDir = layout.buildDirectory.dir("kotlin")
3939
apiPackage = "org.openapitools.example.api"
4040
invokerPackage = "org.openapitools.example.invoker"
4141
modelPackage = "org.openapitools.example.model"
@@ -57,8 +57,8 @@ openApiGenerate {
5757
task buildJavaResttemplateSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
5858
generatorName = "java"
5959
library = "resttemplate"
60-
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
61-
outputDir = layout.buildDirectory.dir("java-resttemplate-api-client").get().asFile.toString()
60+
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
61+
outputDir = layout.buildDirectory.dir("java-resttemplate-api-client")
6262
apiPackage = "com.example.client"
6363
invokerPackage = "com.example.invoker"
6464
modelPackage = "com.example.cdm"
@@ -75,24 +75,24 @@ task buildJavaResttemplateSdk(type: org.openapitools.generator.gradle.plugin.tas
7575

7676
task buildGoSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
7777
generatorName = "go"
78-
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
78+
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
7979
additionalProperties = [
8080
packageName: "petstore"
8181
]
82-
outputDir = layout.buildDirectory.dir("go").get().asFile.toString()
82+
outputDir = layout.buildDirectory.dir("go")
8383
configOptions = [
8484
dateLibrary: "threetenp"
8585
]
8686
}
8787

8888
task buildDotnetSdk(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
8989
generatorName = "csharp"
90-
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
90+
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
9191
additionalProperties = [
9292
packageGuid: "{321C8C3F-0156-40C1-AE42-D59761FB9B6C}",
9393
useCompareNetObjects: "true"
9494
]
95-
outputDir = layout.buildDirectory.dir("csharp").get().asFile.toString()
95+
outputDir = layout.buildDirectory.dir("csharp")
9696
globalProperties = [
9797
models: "",
9898
apis : "",
@@ -102,22 +102,22 @@ task buildDotnetSdk(type: org.openapitools.generator.gradle.plugin.tasks.Generat
102102
task generateGoWithInvalidSpec(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask){
103103
validateSpec = true
104104
generatorName = "go"
105-
inputSpec = "$rootDir/petstore-v3.0-invalid.yaml".toString()
105+
inputSpec = layout.projectDirectory.file("petstore-v3.0-invalid.yaml")
106106
additionalProperties = [
107107
packageName: "petstore"
108108
]
109-
outputDir = layout.buildDirectory.dir("go").get().asFile.toString()
109+
outputDir = layout.buildDirectory.dir("go")
110110
configOptions = [
111111
dateLibrary: "threetenp"
112112
]
113113
}
114114

115115
task validateGoodSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
116-
inputSpec = "$rootDir/petstore-v3.0.yaml".toString()
116+
inputSpec = layout.projectDirectory.file("petstore-v3.0.yaml")
117117
}
118118

119119
task validateBadSpec(type: org.openapitools.generator.gradle.plugin.tasks.ValidateTask){
120-
inputSpec = "$rootDir/petstore-v3.0-invalid.yaml".toString()
120+
inputSpec = layout.projectDirectory.file("petstore-v3.0-invalid.yaml")
121121
}
122122

123123
task validateSpecs(dependsOn: ['validateGoodSpec', 'validateBadSpec'])

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
6060
project
6161
)
6262

63-
generate.outputDir.set(project.layout.buildDirectory.dir("generate-resources/main").map { it.asFile.path })
63+
// FIX: Use modern layout API and .convention() for defaults instead of .set()
64+
generate.outputDir.convention(layout.buildDirectory.dir("generate-resources/main"))
6465

6566
tasks.apply {
6667
register("openApiGenerators", GeneratorsTask::class.java).configure {

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

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20-
import org.gradle.api.tasks.Input
21-
import org.gradle.api.tasks.Optional
20+
import org.gradle.api.file.DirectoryProperty
21+
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
@@ -28,7 +28,7 @@ import org.gradle.kotlin.dsl.property
2828
*
2929
* @author Jim Schubert
3030
*/
31-
open class OpenApiGeneratorGenerateExtension(project: Project) {
31+
open class OpenApiGeneratorGenerateExtension(private val project: Project) {
3232
/**
3333
* The verbosity of generation
3434
*/
@@ -47,7 +47,7 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
4747
/**
4848
* The output target directory into which code will be generated.
4949
*/
50-
val outputDir = project.objects.property<String>()
50+
val outputDir: DirectoryProperty = project.objects.directoryProperty()
5151

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

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

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

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

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

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

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

284284
/**
285285
* Remove prefix of operationId, e.g. config_getId => getId
@@ -413,22 +413,58 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
413413

414414
@Suppress("MemberVisibilityCanBePrivate")
415415
fun applyDefaults() {
416-
releaseNote.set("Minor update")
417-
inputSpecRootDirectorySkipMerge.set(false)
418-
modelNamePrefix.set("")
419-
modelNameSuffix.set("")
420-
apiNameSuffix.set("")
421-
generateModelTests.set(true)
422-
generateModelDocumentation.set(true)
423-
generateApiTests.set(true)
424-
generateApiDocumentation.set(true)
425-
configOptions.set(mapOf())
426-
validateSpec.set(true)
427-
logToStderr.set(false)
428-
enablePostProcessFile.set(false)
429-
skipValidateSpec.set(false)
430-
generateAliasAsModel.set(false)
431-
cleanupOutput.set(false)
432-
dryRun.set(false)
416+
releaseNote.convention("Minor update")
417+
inputSpecRootDirectorySkipMerge.convention(false)
418+
modelNamePrefix.convention("")
419+
modelNameSuffix.convention("")
420+
apiNameSuffix.convention("")
421+
generateModelTests.convention(true)
422+
generateModelDocumentation.convention(true)
423+
generateApiTests.convention(true)
424+
generateApiDocumentation.convention(true)
425+
configOptions.convention(mapOf())
426+
validateSpec.convention(true)
427+
logToStderr.convention(false)
428+
enablePostProcessFile.convention(false)
429+
skipValidateSpec.convention(false)
430+
generateAliasAsModel.convention(false)
431+
cleanupOutput.convention(false)
432+
dryRun.convention(false)
433433
}
434-
}
434+
435+
// ========================================================================
436+
// Backwards-compatibility bridge setters for Groovy/Kotlin DSL
437+
// These allow users to continue assigning paths as standard strings.
438+
// ========================================================================
439+
440+
/** Backwards-compatibility bridge for outputDir */
441+
fun setOutputDir(path: String) {
442+
outputDir.set(project.layout.projectDirectory.dir(path))
443+
}
444+
445+
/** Backwards-compatibility bridge for inputSpec */
446+
fun setInputSpec(path: String) {
447+
inputSpec.set(project.layout.projectDirectory.file(path))
448+
}
449+
450+
/** Backwards-compatibility bridge for inputSpecRootDirectory */
451+
fun setInputSpecRootDirectory(path: String) {
452+
inputSpecRootDirectory.set(project.layout.projectDirectory.dir(path))
453+
}
454+
455+
/** Backwards-compatibility bridge for templateDir */
456+
fun setTemplateDir(path: String) {
457+
templateDir.set(project.layout.projectDirectory.dir(path))
458+
}
459+
460+
/** Backwards-compatibility bridge for configFile */
461+
fun setConfigFile(path: String) {
462+
configFile.set(project.layout.projectDirectory.file(path))
463+
}
464+
465+
/** Backwards-compatibility bridge for ignoreFileOverride */
466+
fun setIgnoreFileOverride(path: String) {
467+
ignoreFileOverride.set(project.layout.projectDirectory.file(path))
468+
}
469+
470+
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.provider.ListProperty
2021
import org.gradle.kotlin.dsl.listProperty
2122
import org.openapitools.codegen.meta.Stability
2223

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

3435
init {
3536
applyDefaults()
3637
}
3738

3839
@Suppress("MemberVisibilityCanBePrivate")
39-
fun applyDefaults() =
40-
include.set(Stability.values().map { it.value() }.filterNot { it == Stability.DEPRECATED.value() })
41-
}
40+
fun applyDefaults() {
41+
// Use .convention() instead of .set() so users can cleanly override this default
42+
include.convention(
43+
Stability.values()
44+
.map { it.value() }
45+
.filterNot { it == Stability.DEPRECATED.value() }
46+
)
47+
}
48+
49+
// ========================================================================
50+
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
51+
// Allows users to continue assigning lists directly via `=`
52+
// ========================================================================
53+
fun setInclude(items: Iterable<String>) {
54+
include.set(items)
55+
}
56+
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,32 +17,47 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.file.DirectoryProperty
21+
import org.gradle.api.provider.Property
2022
import org.gradle.kotlin.dsl.property
2123

2224
/**
2325
* Gradle project level extension object definition for the meta-generator task
2426
*
2527
* @author Jim Schubert
2628
*/
27-
open class OpenApiGeneratorMetaExtension(project: Project) {
29+
open class OpenApiGeneratorMetaExtension(private val project: Project) {
2830
/**
2931
* The human-readable generator name of the newly created template generator.
3032
*/
31-
val generatorName = project.objects.property<String>()
33+
val generatorName: Property<String> = project.objects.property()
3234

3335
/**
3436
* The packageName generatorName to put the main class into (defaults to org.openapitools.codegen)
3537
*/
36-
val packageName = project.objects.property<String>()
38+
val packageName: Property<String> = project.objects.property()
3739

3840
/**
3941
* Where to write the generated files (current dir by default).
4042
*/
41-
val outputFolder = project.objects.property<String>()
43+
val outputFolder: DirectoryProperty = project.objects.directoryProperty()
4244

4345
init {
44-
generatorName.set("default")
45-
packageName.set("org.openapitools.codegen")
46-
outputFolder.set("")
46+
// Use .convention() to allow users to cleanly override these defaults
47+
generatorName.convention("default")
48+
packageName.convention("org.openapitools.codegen")
49+
50+
// Use the native layout project directory instead of an empty string
51+
outputFolder.convention(project.layout.projectDirectory)
52+
}
53+
54+
// ========================================================================
55+
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
56+
// Allows users to continue assigning paths as standard strings.
57+
// ========================================================================
58+
59+
/** Backwards-compatibility bridge for outputFolder */
60+
fun setOutputFolder(path: String) {
61+
outputFolder.set(project.layout.projectDirectory.dir(path))
4762
}
4863
}

0 commit comments

Comments
 (0)