Skip to content

Commit 542545b

Browse files
committed
update README.adoc
1 parent ae11608 commit 542545b

1 file changed

Lines changed: 122 additions & 1 deletion

File tree

modules/openapi-generator-gradle-plugin/README.adoc

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,67 @@ Run with --stacktrace option to get the stack trace. Run with --info or --debug
681681
$ ./gradlew openApiValidate --input=/Users/jim/projects/openapi-generator/modules/openapi-generator/src/test/resources/3_0/petstore.yaml
682682
----
683683

684+
=== Groovy DSL Bridge Methods
685+
686+
Both project extensions and custom task instances provide bridge methods that allow Groovy DSL users to set file and directory properties using simple String paths. These bridge methods support both **method-style** and **property-style** syntax.
687+
688+
==== Extension Bridge Methods (for openApiGenerate, openApiValidate, openApiMeta)
689+
690+
When configuring project extensions like `openApiGenerate { }`, `openApiValidate { }`, or `openApiMeta { }`, Groovy DSL users can use bridge methods to set properties with String paths.
691+
692+
.Available Extension Bridge Methods
693+
|===
694+
|Extension |Available Methods
695+
696+
|`openApiGenerate`
697+
|`setInputSpec(path)`, `setOutputDir(path)`, `setTemplateDir(path)`, `setConfigFile(path)`, `setIgnoreFileOverride(path)`, `setInputSpecRootDirectory(path)`
698+
699+
|`openApiValidate`
700+
|`setInputSpec(path)`
701+
702+
|`openApiMeta`
703+
|`setOutputFolder(path)`
704+
|===
705+
706+
.Example: Using extension bridge methods in Groovy DSL
707+
[source,groovy]
708+
----
709+
// Method-style syntax
710+
openApiGenerate {
711+
generatorName.set("kotlin")
712+
setInputSpec("$rootDir/specs/petstore.yaml")
713+
setOutputDir("build/generated")
714+
apiPackage.set("org.openapi.example.api")
715+
}
716+
717+
// Property-style syntax (Groovy allows this syntactic sugar)
718+
openApiGenerate {
719+
generatorName.set("kotlin")
720+
inputSpec = "$rootDir/specs/petstore.yaml"
721+
outputDir = "build/generated"
722+
apiPackage.set("org.openapi.example.api")
723+
}
724+
725+
// Also works with openApiValidate
726+
openApiValidate {
727+
inputSpec = "$rootDir/specs/petstore.yaml"
728+
recommend.set(true)
729+
}
730+
731+
// And openApiMeta
732+
openApiMeta {
733+
generatorName.set("MyGenerator")
734+
outputFolder = "build/custom-generator"
735+
}
736+
----
737+
738+
[IMPORTANT]
739+
====
740+
**Smart Input Routing:** The `setInputSpec()` method (in both extensions and tasks) automatically detects whether the provided path is a remote URL (e.g., `http://`, `https://`, `jar:`) or a local file path, and routes it to the appropriate property (`remoteInputSpec` or `inputSpec`).
741+
742+
Additionally, when you set a local file path after previously setting a remote URL (or vice versa), the method automatically clears the conflicting property to prevent stale values from taking precedence. This ensures your configuration behaves predictably even when you change between local and remote specs.
743+
====
744+
684745
=== Generate multiple sources
685746

686747
If you want to perform multiple generation tasks, you'd want to create a task that inherits from the `GenerateTask`.
@@ -716,6 +777,57 @@ task buildKotlinClient(type: org.openapitools.generator.gradle.plugin.tasks.Gene
716777
}
717778
```
718779

780+
==== Custom Task Bridge Methods (AsString suffix)
781+
782+
When creating custom task instances (as shown above), Groovy DSL users also have access to bridge methods with the `AsString` suffix. These methods provide the same functionality as the extension bridge methods.
783+
784+
[NOTE]
785+
====
786+
**Why the "AsString" suffix?**
787+
788+
Due to technical limitations with Gradle's abstract property getters in task classes, the bridge methods for custom tasks require the `AsString` suffix to avoid naming conflicts with Gradle's internal getter methods.
789+
790+
- **Extensions** (e.g., `openApiGenerate { }`) use methods like `setInputSpec()`, `setOutputDir()`
791+
- **Custom Tasks** (e.g., `task myTask(type: GenerateTask) { }`) use methods like `setInputSpecAsString()`, `setOutputDirAsString()`
792+
793+
Both patterns work identically for String-based property configuration in Groovy DSL.
794+
====
795+
796+
.Available AsString Bridge Methods for Custom Tasks
797+
|===
798+
|Task Type |Available Methods
799+
800+
|`GenerateTask`
801+
|`setInputSpecAsString(path)`, `setOutputDirAsString(path)`, `setTemplateDirAsString(path)`, `setConfigFileAsString(path)`, `setIgnoreFileOverrideAsString(path)`, `setInputSpecRootDirectoryAsString(path)`, `setSchemaLocationAsString(path)`
802+
803+
|`ValidateTask`
804+
|`setInputSpecAsString(path)`
805+
806+
|`MetaTask`
807+
|`setOutputFolderAsString(path)`
808+
|===
809+
810+
.Example: Using AsString methods in Groovy DSL
811+
[source,groovy]
812+
----
813+
// Method-style syntax
814+
task buildCustomClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
815+
generatorName.set("kotlin")
816+
setInputSpecAsString("$rootDir/specs/api.yaml")
817+
setOutputDirAsString("build/generated/custom")
818+
setTemplateDirAsString("templates/custom")
819+
apiPackage.set("com.example.api")
820+
}
821+
822+
// Property-style syntax (Groovy allows this syntactic sugar)
823+
task buildAnotherClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
824+
generatorName.set("java")
825+
inputSpecAsString = "$rootDir/specs/another-api.yaml"
826+
outputDirAsString = "build/generated/another"
827+
modelPackage.set("com.example.model")
828+
}
829+
----
830+
719831
To execute your specs, you'd then do:
720832

721833
```
@@ -770,7 +882,16 @@ openApiGenerate {
770882
}
771883
----
772884

773-
*Note: Standard string assignments (e.g., `inputSpec = "path/to/file.yaml"`) are still fully supported via bridge methods for static files checked into your repository.*
885+
[NOTE]
886+
====
887+
**Provider API vs Bridge Methods:**
888+
889+
For optimal lazy configuration and task wiring, both Groovy and Kotlin DSL users should use `.set()` with the Provider API when possible (e.g., `inputSpec.set(downloadSpec.flatMap { it.outputFile })`).
890+
891+
For simpler cases with static String paths, Groovy DSL users can use the convenient bridge methods:
892+
- Extension configuration: Use methods like `setInputSpec()`, `setOutputDir()` (see "Extension Bridge Methods" section)
893+
- Custom task configuration: Use methods like `setInputSpecAsString()`, `setOutputDirAsString()` (see "Custom Task Bridge Methods" section)
894+
====
774895

775896
== Troubleshooting
776897

0 commit comments

Comments
 (0)