Skip to content

Commit 9e33799

Browse files
committed
Support spec files which are written in yaml
1 parent ea6d332 commit 9e33799

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

openapigenerator-gradle-plugin/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = "0.0.2"
1+
version = "0.1.0"
22

33
plugins {
44
`java-gradle-plugin`
@@ -11,7 +11,8 @@ plugins {
1111
dependencies {
1212
implementation(gradleKotlinDsl())
1313

14-
api(project(":openapigenerator"))
14+
implementation(project(":openapigenerator"))
15+
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.2")
1516
}
1617

1718
pluginBundle {

openapigenerator-gradle-plugin/src/main/kotlin/net/axay/openapigenerator/OpenApiGenerateTask.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.axay.openapigenerator
22

3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
35
import org.gradle.api.DefaultTask
46
import org.gradle.api.file.RegularFileProperty
57
import org.gradle.api.provider.Property
@@ -24,6 +26,15 @@ abstract class OpenApiGenerateTask : DefaultTask() {
2426
@get:Input
2527
abstract val specFile: RegularFileProperty
2628

29+
/**
30+
* The file format of the spec, e.g. `json` or `yaml`.
31+
* This only needs to specified if the spec file itself has
32+
* no file extension.
33+
*/
34+
@get:Optional
35+
@get:Input
36+
abstract val specFormat: Property<String>
37+
2738
/**
2839
* The name of the package where all files will be generated in.
2940
* The generator might add sub packages inside this package.
@@ -46,12 +57,23 @@ abstract class OpenApiGenerateTask : DefaultTask() {
4657

4758
@TaskAction
4859
fun generate() {
49-
val openApiJson = when {
60+
val openApiText = when {
5061
specFile.isPresent -> specFile.get().asFile.readText()
5162
specUrl.isPresent -> URL(specUrl.get()).readText()
5263
else -> error("Both 'specFile' and 'specUrl' have not been set, but one them is required for resolving the OpenAPI spec!")
5364
}
5465

66+
val yamlTypes = listOf("yml", "yaml")
67+
val isYaml = when {
68+
specFormat.isPresent -> specFormat.get() in yamlTypes
69+
specFile.isPresent -> specFile.get().asFile.extension in yamlTypes
70+
specUrl.isPresent -> specUrl.get().split(".").lastOrNull() in yamlTypes
71+
else -> error("Unreachable state")
72+
}
73+
val openApiJson = if (isYaml) {
74+
ObjectMapper(YAMLFactory()).readTree(openApiText).toString()
75+
} else openApiText
76+
5577
val outputDirectoryFile = outputDirectory.get().asFile
5678
if (deleteOldOutput.getOrElse(false)) {
5779
outputDirectoryFile.deleteRecursively()

0 commit comments

Comments
 (0)