From 2b04a5a12784cba73de401c5e8c69203ec4d2b2d Mon Sep 17 00:00:00 2001 From: Ivan Porto Carrero Date: Mon, 26 Aug 2013 17:55:28 -0700 Subject: [PATCH] Fix using the typeMappings --- build.sbt | 12 ++++- .../com/wordnik/swagger/codegen/Codegen.scala | 2 +- .../codegen/ScalaAsyncClientGenerator.scala | 23 +++++++-- .../codegen/language/CodegenConfig.scala | 2 +- .../model/SwaggerModelSerializer.scala | 6 +-- .../swaggerSpec1_2/ModelSerializersTest.scala | 48 +++++++++++++++++++ 6 files changed, 82 insertions(+), 11 deletions(-) diff --git a/build.sbt b/build.sbt index bcf0cd82c0..00616b0183 100644 --- a/build.sbt +++ b/build.sbt @@ -5,9 +5,9 @@ organization := "com.wordnik" name := "swagger-codegen" -version := "2.0.9-SNAPSHOT" +version := "2.0.9-WN8" -scalaVersion := "2.9.1" +scalaVersion := "2.10.0" javacOptions ++= Seq("-target", "1.6", "-source", "1.6", "-Xlint:unchecked", "-Xlint:deprecation") @@ -55,6 +55,14 @@ publishTo <<= (version) { version: String => Some("Sonatype Nexus Releases" at "https://oss.sonatype.org/service/local/staging/deploy/maven2") } +publishTo <<= (version) { version: String => + val artifactory = "https://ci.aws.wordnik.com/artifactory/m2-" + if (version.trim.endsWith("SNAPSHOT")) + Some("snapshots" at artifactory + "snapshots") + else + Some("releases" at artifactory + "releases") +} + //publishTo := Some(Resolver.file("file", new File(Path.userHome.absolutePath+"/.m2/repository"))) diff --git a/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala b/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala index ac245e777e..664b62de02 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/Codegen.scala @@ -406,7 +406,7 @@ class Codegen(config: CodegenConfig) { baseType = config.typeMapping.contains(baseType) match { case true => config.typeMapping(baseType) case false => { - imports += Map("import" -> config.typeMapping.getOrElse(baseType, baseType)) + imports += Map("import" -> config.toDeclaredType(baseType)) baseType } } diff --git a/src/main/scala/com/wordnik/swagger/codegen/ScalaAsyncClientGenerator.scala b/src/main/scala/com/wordnik/swagger/codegen/ScalaAsyncClientGenerator.scala index 43572f19b2..1c4631f149 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/ScalaAsyncClientGenerator.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/ScalaAsyncClientGenerator.scala @@ -13,6 +13,7 @@ import com.wordnik.swagger.codegen.util.{CoreUtils, ApiExtractor, ResourceExtrac import com.wordnik.swagger.codegen.spec.SwaggerSpecValidator import mojolly.inflector.InflectorImports._ import org.rogach.scallop.{ScallopConf, Scallop} +import scala.annotation.switch case class SwaggerApi( clientName: String, @@ -266,13 +267,15 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator { "boolean" -> "Boolean", "string" -> "String", "int" -> "Int", + "long" -> "Long", "float" -> "Float", "byte" -> "Byte", "short" -> "Short", "char" -> "Char", "long" -> "Long", "double" -> "Double", - "object" -> "Any") ++ cfg.typeMapping + "object" -> "Any", + "file" -> "File") ++ cfg.typeMapping override val defaultIncludes = Set( "Int", @@ -482,20 +485,32 @@ class ScalaAsyncClientGenerator(cfg: SwaggerGenConfig) extends BasicGenerator { override def processResponseClass(responseClass: String): Option[String] = { responseClass match { case "void" => None //Some("Unit") - case e: String => Some(typeMapping.getOrElse(e, e)) + case e: String => Some(toDeclaredType(e)) } } override def processResponseDeclaration(responseClass: String): Option[String] = { responseClass match { case "void" => None //Some("Unit") - case e: String => Some(typeMapping.getOrElse(e, e)) + case e: String => Some(toDeclaredType(e)) } } + override def toDeclaredType(dt: String): String = { + val declaredType = (dt.indexOf("["): @switch) match { + case -1 => dt + case n: Int => { + if (dt.substring(0, n).toLowerCase == "array") + "List" + dt.substring(n) + else dt + } + } + typeMapping.getOrElse(declaredType, declaredType) + } + override def toDeclaration(obj: ModelProperty): (String, String) = { obj.`type` match { - case "Array" => { + case "Array" | "array" => { val inner = { obj.items match { case Some(items) => items.ref.getOrElse(items.`type`) diff --git a/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala b/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala index 1888d7e178..d55207cbec 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala @@ -74,7 +74,7 @@ abstract class CodegenConfig { def processResponseDeclaration(responseClass: String): Option[String] = { responseClass match { case "void" => None - case e: String => Some(typeMapping.getOrElse(e, e)) + case e: String => Some(toDeclaredType(e)) } } diff --git a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala index ea0ebf59ac..8fe8d47f78 100644 --- a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala +++ b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala @@ -425,7 +425,7 @@ object SwaggerSerializers { case json => val output = new LinkedHashMap[String, ModelProperty] val required = (json \ "required").extract[Set[String]] - val properties = (json \ "properties") match { + json \ "properties" match { case JObject(entries) => { entries.map({ case (key, value) => { @@ -464,7 +464,7 @@ object SwaggerSerializers { case _ => Extraction.decompose(required) })) ~ ("properties" -> { - x.properties match { + (x.properties: @unchecked) match { case e: LinkedHashMap[String, ModelProperty] => Extraction.decompose(e.toMap) case _ => JNothing } @@ -485,7 +485,7 @@ object SwaggerSerializers { } val output = new ListBuffer[String] - (json \ "enum") match { + json \ "enum" match { case JArray(entries) => entries.map { case e: JInt => output += e.num.toString case e: JBool => output += e.value.toString diff --git a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala index 89afa0ab0f..3fe03cd5b6 100644 --- a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala +++ b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala @@ -256,6 +256,54 @@ class OperationSerializersTest extends FlatSpec with ShouldMatchers { case _ => fail("wrong type returned, should be Operation") } } + it should "deserialize an Operation with an array property" in { + val jsonString = """ + { + "method":"GET", + "summary":"the summary", + "notes":"the notes", + "type":"string", + "nickname":"getMeSomePets", + "parameters":[ + { + "name":"id", + "description":"the id", + "defaultValue":"-1", + "required":false, + "allowMultiple":true, + "type":"array", + "items": { + "$ref": "Pet" + }, + "enum":["a","b","c"], + "paramType":"query" + } + ] + } + """ + val json = parse(jsonString) + json.extract[Operation] match { + case op: Operation => { + op.method should be ("GET") + op.summary should be ("the summary") + op.notes should be ("the notes") + op.responseClass should be ("array[Pet]") + op.nickname should be ("getMeSomePets") + op.parameters.size should be (1) + + op.parameters.foreach(m => { + m.name should be ("id") + m.description should be (Some("the id")) + m.defaultValue should be (Some("-1")) + m.required should be (false) + m.allowMultiple should be (true) + m.dataType should be ("string") + m.paramType should be ("query") + }) + } + case _ => fail("wrong type returned, should be Operation") + } + } it should "serialize an operation" in { val op = Operation(