From 01dc29d7890c38dcea5a9f45fc318823692613b8 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 22 Aug 2013 13:18:03 -0700 Subject: [PATCH] fix for jsonschema format in model property with array --- .../model/SwaggerModelSerializer.scala | 16 +++++- .../swaggerSpec1_2/ModelSerializersTest.scala | 53 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala index 5b707dda602..7e9a2dda93d 100644 --- a/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala +++ b/src/main/scala/com/wordnik/swagger/model/SwaggerModelSerializer.scala @@ -567,8 +567,22 @@ object SwaggerSerializers { class ModelRefSerializer extends CustomSerializer[ModelRef](formats => ({ case json => implicit val fmts: Formats = formats + + val `type` = (json \ "type") match { + case e: JString => e.s + case _ => "" + } + val format = (json \ "format") match { + case e: JString => e.s + case _ => "" + } + val jsonSchemaType = jsonSchemaTypeMap.getOrElse((`type`, format), `type`) + ModelRef( - (json \ "type").extractOrElse(null: String), + jsonSchemaType match { + case e: String if(e != "") => e + case _ => null + }, (json \ "$ref").extractOpt[String] ) }, { diff --git a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala index 332b25864b8..89afa0ab0f7 100644 --- a/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala +++ b/src/test/scala/swaggerSpec1_2/ModelSerializersTest.scala @@ -569,6 +569,59 @@ class ModelPropertySerializationTest extends FlatSpec with ShouldMatchers { val p = ModelProperty("string", "string", 0, false, Some("nice")) write(p) should be ("""{"type":"string","description":"nice"}""") } + + it should "extract model properties" in { + val jsonString = """ + { + "type":"integer", + "format":"int64", + "required":true, + "description":"nice" + } + """ + val json = parse(jsonString) + json.extract[ModelProperty] match { + case p: ModelProperty => { + p.`type` should be ("long") + p.required should be (true) + p.description should be (Some("nice")) + } + case _ => fail("expected type ModelProperty") + } + } + + it should "extract model properties with arrays" in { + val jsonString = """ +{ + "id": "DocIdList", + "name": "DocIdList", + "properties": { + "docIds": { + "items": { + "format": "int64", + "type": "integer" + }, + "type": "array" + } + } +} +""" + val json = parse(jsonString) + json.extract[Model] match { + case p: Model => { + p.properties should not be (null) + p.properties.size should be (1) + p.properties.keys.size should be (1) + for(key <- p.properties.keys) { + val property = p.properties(key) + property.`type` should be ("Array") + property.items should not be (None) + property.items.get.`type` should be ("long") + } + } + case _ => fail("expected type ModelProperty") + } + } } @RunWith(classOf[JUnitRunner])