11package net.axay.openapigenerator
22
3+ import com.fasterxml.jackson.databind.ObjectMapper
4+ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
35import org.gradle.api.DefaultTask
46import org.gradle.api.file.RegularFileProperty
57import 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