forked from loafle/openapi-generator-original
added spec converter
This commit is contained in:
parent
aa1fbb680b
commit
f6622fbb59
@ -0,0 +1,57 @@
|
|||||||
|
package com.wordnik.swagger.codegen
|
||||||
|
|
||||||
|
import com.wordnik.swagger.codegen.util.{ ResourceExtractor, ApiExtractor }
|
||||||
|
import com.wordnik.swagger.model._
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
import org.json4s._
|
||||||
|
import org.json4s.jackson.JsonMethods._
|
||||||
|
import org.json4s.jackson.Serialization.{ read, write }
|
||||||
|
|
||||||
|
object SpecConverter {
|
||||||
|
def main(args: Array[String]) = {
|
||||||
|
implicit val formats = SwaggerSerializers.formats("1.2")
|
||||||
|
|
||||||
|
val url = args(0)
|
||||||
|
val key = Option(args(1))
|
||||||
|
val outputDir = new File(args(2))
|
||||||
|
|
||||||
|
if(!outputDir.exists) outputDir.mkdir
|
||||||
|
|
||||||
|
val resourcePath = url.split("/").last
|
||||||
|
val resourceListing = ResourceExtractor.fetchListing(url, key)
|
||||||
|
|
||||||
|
val updatedListing = {
|
||||||
|
val apis = (for(api <- resourceListing.apis) yield {
|
||||||
|
val path = if(api.path.startsWith("/" + resourcePath)) {
|
||||||
|
api.path.substring(resourcePath.length + 1)
|
||||||
|
}
|
||||||
|
else api.path
|
||||||
|
api.copy(path = path.replace(".{format}",""))
|
||||||
|
}).toList
|
||||||
|
resourceListing.copy(apis = apis, swaggerVersion = "1.2")
|
||||||
|
}
|
||||||
|
writeToFile(outputDir + File.separator + "api-docs", write(updatedListing))
|
||||||
|
|
||||||
|
val listings = ApiExtractor.fetchApiListings(resourceListing.swaggerVersion, resourceListing.basePath, resourceListing.apis, key)
|
||||||
|
|
||||||
|
listings.foreach(listing => {
|
||||||
|
val apis = (for(api <- listing.apis) yield {
|
||||||
|
api.copy(path = api.path.replace(".{format}", ""))
|
||||||
|
})
|
||||||
|
val filename = listing.resourcePath.replace("/","")
|
||||||
|
val updatedApi = listing.copy(swaggerVersion = "1.2", apis = apis)
|
||||||
|
writeToFile(outputDir + File.separator + filename, write(updatedApi))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
def writeToFile(p: String, s: String) {
|
||||||
|
val pw = new java.io.PrintWriter(new File(p))
|
||||||
|
try {
|
||||||
|
pw.write(s)
|
||||||
|
} finally {
|
||||||
|
pw.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,5 +18,5 @@ package com.wordnik.swagger.codegen.spec
|
|||||||
|
|
||||||
object SwaggerSpec {
|
object SwaggerSpec {
|
||||||
val primitives = List("int", "string", "long", "double", "float", "boolean", "void")
|
val primitives = List("int", "string", "long", "double", "float", "boolean", "void")
|
||||||
val containers = List("List", "Map", "Set", "Array")
|
val containers = List("List", "Map", "Set", "Array", "array")
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,7 @@ object SwaggerSerializers {
|
|||||||
case "Date" => (name -> "string") ~ ("format" -> "date-time")
|
case "Date" => (name -> "string") ~ ("format" -> "date-time")
|
||||||
case "date" => (name -> "string") ~ ("format" -> "date")
|
case "date" => (name -> "string") ~ ("format" -> "date")
|
||||||
case "date-time" => (name -> "string") ~ ("format" -> "date-time")
|
case "date-time" => (name -> "string") ~ ("format" -> "date-time")
|
||||||
|
case "Array" => (name -> "array")
|
||||||
case _ => {
|
case _ => {
|
||||||
val ComplexTypeMatcher = "([a-zA-Z]*)\\[([a-zA-Z\\.\\-]*)\\].*".r
|
val ComplexTypeMatcher = "([a-zA-Z]*)\\[([a-zA-Z\\.\\-]*)\\].*".r
|
||||||
`type` match {
|
`type` match {
|
||||||
@ -138,7 +139,7 @@ object SwaggerSerializers {
|
|||||||
}) ~
|
}) ~
|
||||||
("models" -> {
|
("models" -> {
|
||||||
x.models match {
|
x.models match {
|
||||||
case e: Map[String, Model] if (e.size > 0) => Extraction.decompose(e)
|
case Some(e) if (e.size > 0) => Extraction.decompose(e)
|
||||||
case _ => JNothing
|
case _ => JNothing
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -168,7 +169,6 @@ object SwaggerSerializers {
|
|||||||
implicit val fmts = formats
|
implicit val fmts = formats
|
||||||
("apiVersion" -> x.apiVersion) ~
|
("apiVersion" -> x.apiVersion) ~
|
||||||
("swaggerVersion" -> x.swaggerVersion) ~
|
("swaggerVersion" -> x.swaggerVersion) ~
|
||||||
("basePath" -> x.basePath) ~
|
|
||||||
("apis" -> {
|
("apis" -> {
|
||||||
x.apis match {
|
x.apis match {
|
||||||
case e: List[ApiListingReference] if (e.size > 0) => Extraction.decompose(e)
|
case e: List[ApiListingReference] if (e.size > 0) => Extraction.decompose(e)
|
||||||
@ -381,12 +381,12 @@ object SwaggerSerializers {
|
|||||||
}, {
|
}, {
|
||||||
case x: Parameter =>
|
case x: Parameter =>
|
||||||
implicit val fmts = formats
|
implicit val fmts = formats
|
||||||
("name" -> x.name) ~
|
val output = ("name" -> x.name) ~
|
||||||
("description" -> x.description) ~
|
("description" -> x.description) ~
|
||||||
("defaultValue" -> x.defaultValue) ~
|
("defaultValue" -> x.defaultValue) ~
|
||||||
("required" -> x.required) ~
|
("required" -> x.required) ~
|
||||||
("allowMultiple" -> x.allowMultiple) ~
|
("allowMultiple" -> x.allowMultiple) ~
|
||||||
("type" -> x.dataType) ~
|
toJsonSchema("type", x.dataType) ~
|
||||||
("allowableValues" -> {
|
("allowableValues" -> {
|
||||||
x.allowableValues match {
|
x.allowableValues match {
|
||||||
case AnyAllowableValues => JNothing // don't serialize when not a concrete type
|
case AnyAllowableValues => JNothing // don't serialize when not a concrete type
|
||||||
@ -395,6 +395,14 @@ object SwaggerSerializers {
|
|||||||
}
|
}
|
||||||
}) ~
|
}) ~
|
||||||
("paramType" -> x.paramType)
|
("paramType" -> x.paramType)
|
||||||
|
|
||||||
|
x.allowableValues match {
|
||||||
|
case AllowableListValues(values, "LIST") =>
|
||||||
|
output ~ ("enum" -> Extraction.decompose(values))
|
||||||
|
case AllowableRangeValues(min, max) =>
|
||||||
|
output ~ ("minimum" -> min) ~ ("maximum" -> max)
|
||||||
|
case _ => output
|
||||||
|
}
|
||||||
}
|
}
|
||||||
))
|
))
|
||||||
|
|
||||||
@ -431,8 +439,17 @@ object SwaggerSerializers {
|
|||||||
}, {
|
}, {
|
||||||
case x: Model =>
|
case x: Model =>
|
||||||
implicit val fmts = formats
|
implicit val fmts = formats
|
||||||
|
val required: List[String] = (for((name, prop) <- x.properties) yield {
|
||||||
|
if(prop.required) Some(name)
|
||||||
|
else None
|
||||||
|
}).flatten.toList
|
||||||
|
|
||||||
("id" -> x.id) ~
|
("id" -> x.id) ~
|
||||||
("name" -> x.name) ~
|
("name" -> x.name) ~
|
||||||
|
("required" -> (required.size match {
|
||||||
|
case 0 => JNothing
|
||||||
|
case _ => Extraction.decompose(required)
|
||||||
|
})) ~
|
||||||
("properties" -> {
|
("properties" -> {
|
||||||
x.properties match {
|
x.properties match {
|
||||||
case e: LinkedHashMap[String, ModelProperty] => Extraction.decompose(e.toMap)
|
case e: LinkedHashMap[String, ModelProperty] => Extraction.decompose(e.toMap)
|
||||||
@ -504,8 +521,7 @@ object SwaggerSerializers {
|
|||||||
}, {
|
}, {
|
||||||
case x: ModelProperty =>
|
case x: ModelProperty =>
|
||||||
implicit val fmts = formats
|
implicit val fmts = formats
|
||||||
("type" -> x.`type`) ~
|
toJsonSchema("type", x.`type`) ~
|
||||||
("required" -> x.required) ~
|
|
||||||
("description" -> x.description) ~
|
("description" -> x.description) ~
|
||||||
("allowableValues" -> {
|
("allowableValues" -> {
|
||||||
x.allowableValues match {
|
x.allowableValues match {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user