Skip to content

Commit

Permalink
Merge pull request #23 from macisamuele/maci-read-spec-version-with-c…
Browse files Browse the repository at this point in the history
…odegen-tools

Read spec version from swagger spec via codegen tools
  • Loading branch information
cortinico committed Mar 21, 2019
2 parents 08a09e3 + 7a9d725 commit 2bb871d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
1 change: 0 additions & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies {
implementation("commons-cli:commons-cli:1.4")
implementation("com.google.guava:guava:27.0-jre")
implementation("io.swagger:swagger-codegen:2.3.1")
implementation("org.json:json:20180813")

testImplementation("junit:junit:4.12")
}
Expand Down
20 changes: 9 additions & 11 deletions plugin/src/main/java/com/yelp/codegen/plugin/CodegenPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yelp.codegen.plugin

import com.yelp.codegen.main
import io.swagger.parser.SwaggerParser
import org.gradle.api.DefaultTask
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.Input
Expand All @@ -11,11 +12,7 @@ import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.json.JSONException
import org.json.JSONObject
import org.json.JSONTokener
import java.io.File
import java.io.FileInputStream

const val DEFAULT_PLATFORM = "kotlin"
const val DEFAULT_VERSION = "0.0.0"
Expand Down Expand Up @@ -126,15 +123,16 @@ open class GenerateTask : DefaultTask() {
}

private fun readVersionFromSpecfile(specFile: File) {
try {
FileInputStream(specFile).use {
val jsonObject = JSONTokener(it).nextValue() as JSONObject
val version = jsonObject.getJSONObject("info").getString("version")
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
specVersion = when (val version = swaggerSpec.info.version) {
is String -> {
println("Successfully read version from Swagger Spec file: $version")
specVersion = version
version
}
else -> {
println("Issue in reading version from Swagger Spec file. Falling back to $DEFAULT_VERSION")
DEFAULT_VERSION
}
} catch (e: JSONException) {
System.err.println("Failed to parse $specFile to read spec version")
}
}
}

0 comments on commit 2bb871d

Please sign in to comment.