diff --git a/src/main/scala/com/wordnik/swagger/codegen/SpecConverter.scala b/src/main/scala/com/wordnik/swagger/codegen/SpecConverter.scala new file mode 100644 index 00000000000..418d91e8e3c --- /dev/null +++ b/src/main/scala/com/wordnik/swagger/codegen/SpecConverter.scala @@ -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() + } + } +} \ No newline at end of file diff --git a/src/main/scala/com/wordnik/swagger/codegen/spec/SwaggerSpec.scala b/src/main/scala/com/wordnik/swagger/codegen/spec/SwaggerSpec.scala index e9c3c087262..0c940f46952 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/spec/SwaggerSpec.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/spec/SwaggerSpec.scala @@ -18,5 +18,5 @@ package com.wordnik.swagger.codegen.spec object SwaggerSpec { 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") } diff --git a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala index 1cf93fbaeae..8493d34f9eb 100644 --- a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala +++ b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala @@ -39,6 +39,7 @@ object SwaggerSerializers { case "Date" => (name -> "string") ~ ("format" -> "date-time") case "date" => (name -> "string") ~ ("format" -> "date") case "date-time" => (name -> "string") ~ ("format" -> "date-time") + case "Array" => (name -> "array") case _ => { val ComplexTypeMatcher = "([a-zA-Z]*)\\[([a-zA-Z\\.\\-]*)\\].*".r `type` match { @@ -138,7 +139,7 @@ object SwaggerSerializers { }) ~ ("models" -> { 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 } }) @@ -168,7 +169,6 @@ object SwaggerSerializers { implicit val fmts = formats ("apiVersion" -> x.apiVersion) ~ ("swaggerVersion" -> x.swaggerVersion) ~ - ("basePath" -> x.basePath) ~ ("apis" -> { x.apis match { case e: List[ApiListingReference] if (e.size > 0) => Extraction.decompose(e) @@ -381,12 +381,12 @@ object SwaggerSerializers { }, { case x: Parameter => implicit val fmts = formats - ("name" -> x.name) ~ + val output = ("name" -> x.name) ~ ("description" -> x.description) ~ ("defaultValue" -> x.defaultValue) ~ ("required" -> x.required) ~ ("allowMultiple" -> x.allowMultiple) ~ - ("type" -> x.dataType) ~ + toJsonSchema("type", x.dataType) ~ ("allowableValues" -> { x.allowableValues match { case AnyAllowableValues => JNothing // don't serialize when not a concrete type @@ -395,6 +395,14 @@ object SwaggerSerializers { } }) ~ ("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 => 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) ~ ("name" -> x.name) ~ + ("required" -> (required.size match { + case 0 => JNothing + case _ => Extraction.decompose(required) + })) ~ ("properties" -> { x.properties match { case e: LinkedHashMap[String, ModelProperty] => Extraction.decompose(e.toMap) @@ -504,8 +521,7 @@ object SwaggerSerializers { }, { case x: ModelProperty => implicit val fmts = formats - ("type" -> x.`type`) ~ - ("required" -> x.required) ~ + toJsonSchema("type", x.`type`) ~ ("description" -> x.description) ~ ("allowableValues" -> { x.allowableValues match {