From f1f466b69f0050ef777441ad76cbca8d714d0e94 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Sat, 8 Dec 2012 12:52:03 -0800 Subject: [PATCH] made extracter smarter with bool, int, string --- .../codegen/language/CodegenConfig.scala | 2 + .../model/SwaggerModelSerializer.scala | 20 +++++- src/test/scala/BasicScalaGeneratorTest.scala | 2 +- src/test/scala/ModelSerializersTest.scala | 62 +++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) 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 d4dcf38890..04a76d26bd 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/language/CodegenConfig.scala @@ -29,6 +29,8 @@ abstract class CodegenConfig { def destinationDir: String def toModelName(name: String): String def toApiName(name: String): String + + def toModelFilename(name: String) = name def toApiFilename(name: String) = toApiName(name) def apiNameFromPath(apiPath: String): String diff --git a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala index cec406957a..2492c5b031 100644 --- a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala +++ b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala @@ -167,8 +167,18 @@ object SwaggerSerializers { Parameter( (json \ "name").extractOrElse(""), (json \ "description").extract[String], - (json \ "defaultValue").extractOrElse(""), - (json \ "required").extractOrElse(false), + (json \ "defaultValue") match { + case e:JInt => e.num.toString + case e:JBool => e.value.toString + case e:JString => e.s + case e:JDouble => e.num.toString + case _ => "" + }, + (json \ "required") match { + case e:JString => e.s.toBoolean + case e:JBool => e.value + case _ => false + }, (json \ "allowMultiple").extractOrElse(false), (json \ "dataType").extract[String], (json \ "allowableValues").extract[AllowableValues], @@ -232,7 +242,11 @@ object SwaggerSerializers { implicit val fmts: Formats = formats ModelProperty( `type` = (json \ "type").extractOrElse(""), - required = ((json \ "required").extractOrElse(false)), + (json \ "required") match { + case e:JString => e.s.toBoolean + case e:JBool => e.value + case _ => false + }, description = (json \ "description").extractOpt[String], allowableValues = (json \ "allowableValues").extract[AllowableValues], items = { diff --git a/src/test/scala/BasicScalaGeneratorTest.scala b/src/test/scala/BasicScalaGeneratorTest.scala index 6a6b242b52..4f7ab8cbff 100644 --- a/src/test/scala/BasicScalaGeneratorTest.scala +++ b/src/test/scala/BasicScalaGeneratorTest.scala @@ -251,7 +251,7 @@ class BasicScalaGeneratorTest extends FlatSpec with ShouldMatchers { queryParam("swaggerDataType") should be ("string") queryParam("allowMultiple") should be ("true") queryParam("defaultValue") should be (Some("\"available\"")) - queryParam("allowableValues") should be ("LIST[available,pending,sold]") + queryParam("allowableValues") should be (Some("LIST[available,pending,sold]")) } it should "create an api file" in { diff --git a/src/test/scala/ModelSerializersTest.scala b/src/test/scala/ModelSerializersTest.scala index 8682bafe49..ee9a1e29c0 100644 --- a/src/test/scala/ModelSerializersTest.scala +++ b/src/test/scala/ModelSerializersTest.scala @@ -167,6 +167,24 @@ class ApiDescriptionSerializersTest extends FlatSpec with ShouldMatchers { p.path should be ("/foo/bar") p.description should be ("the description") p.operations.size should be (1) + p.operations.foreach(op => { + op.httpMethod should be ("GET") + op.summary should be ("the summary") + op.notes should be ("the notes") + op.responseClass should be ("string") + op.nickname should be ("getMeSomeStrings") + op.parameters.size should be (1) + + op.parameters.foreach(m => { + m.name should be ("id") + m.description should be ("the id") + m.defaultValue should be ("-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 ApiDescription") } @@ -227,6 +245,16 @@ class OperationSerializersTest extends FlatSpec with ShouldMatchers { op.responseClass should be ("string") op.nickname should be ("getMeSomeStrings") op.parameters.size should be (1) + + op.parameters.foreach(m => { + m.name should be ("id") + m.description should be ("the id") + m.defaultValue should be ("-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") } @@ -276,6 +304,40 @@ class ErrorResponseSerializersTest extends FlatSpec with ShouldMatchers { class ParameterSerializersTest extends FlatSpec with ShouldMatchers { implicit val formats = SwaggerSerializers.formats + it should "deserialize another param" in { + val jsonString = """ + { + "name":"includeDuplicates", + "defaultValue":"false", + "description":"Show duplicate examples from different sources", + "required":"false", + "allowableValues":{ + "values":[ + false, + true + ], + "valueType":"LIST" + }, + "dataType":"string", + "allowMultiple":false, + "paramType":"query" + } + """ + val json = parse(jsonString) + json.extract[Parameter] match { + case p: Parameter => { + p.name should be ("includeDuplicates") + p.description should be ("Show duplicate examples from different sources") + p.defaultValue should be ("false") + p.required should be (false) + p.allowMultiple should be (false) + p.dataType should be ("string") + p.paramType should be ("query") + } + case _ => fail("wrong type returned, should be Parameter") + } + } + it should "deserialize a parameter" in { val jsonString = """ {