Skip to content

Commit 2bb871d

Browse files
authored
Merge pull request #23 from macisamuele/maci-read-spec-version-with-codegen-tools
Read spec version from swagger spec via codegen tools
2 parents 08a09e3 + 7a9d725 commit 2bb871d

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

plugin/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies {
1919
implementation("commons-cli:commons-cli:1.4")
2020
implementation("com.google.guava:guava:27.0-jre")
2121
implementation("io.swagger:swagger-codegen:2.3.1")
22-
implementation("org.json:json:20180813")
2322

2423
testImplementation("junit:junit:4.12")
2524
}

plugin/src/main/java/com/yelp/codegen/plugin/CodegenPlugin.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.yelp.codegen.plugin
22

33
import com.yelp.codegen.main
4+
import io.swagger.parser.SwaggerParser
45
import org.gradle.api.DefaultTask
56
import org.gradle.api.plugins.BasePlugin
67
import org.gradle.api.tasks.Input
@@ -11,11 +12,7 @@ import org.gradle.api.tasks.Optional
1112
import org.gradle.api.tasks.OutputDirectory
1213
import org.gradle.api.tasks.TaskAction
1314
import org.gradle.api.tasks.options.Option
14-
import org.json.JSONException
15-
import org.json.JSONObject
16-
import org.json.JSONTokener
1715
import java.io.File
18-
import java.io.FileInputStream
1916

2017
const val DEFAULT_PLATFORM = "kotlin"
2118
const val DEFAULT_VERSION = "0.0.0"
@@ -126,15 +123,16 @@ open class GenerateTask : DefaultTask() {
126123
}
127124

128125
private fun readVersionFromSpecfile(specFile: File) {
129-
try {
130-
FileInputStream(specFile).use {
131-
val jsonObject = JSONTokener(it).nextValue() as JSONObject
132-
val version = jsonObject.getJSONObject("info").getString("version")
126+
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
127+
specVersion = when (val version = swaggerSpec.info.version) {
128+
is String -> {
133129
println("Successfully read version from Swagger Spec file: $version")
134-
specVersion = version
130+
version
131+
}
132+
else -> {
133+
println("Issue in reading version from Swagger Spec file. Falling back to $DEFAULT_VERSION")
134+
DEFAULT_VERSION
135135
}
136-
} catch (e: JSONException) {
137-
System.err.println("Failed to parse $specFile to read spec version")
138136
}
139137
}
140138
}

0 commit comments

Comments
 (0)