Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read spec version from swagger spec via codegen tools #23

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 0.0.0")
"0.0.0"
macisamuele marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e: JSONException) {
System.err.println("Failed to parse $specFile to read spec version")
}
}
}