Skip to content

Commit 7792e5f

Browse files
author
gbolt
committed
gbolt - minor modifications to SwaggerSerializers and ModelPropertyParser to better unbox Option[_] and Seq[_] types per Spray-Swagger Issue #31
1 parent adf5c1f commit 7792e5f

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

modules/swagger-core/src/main/scala/com/wordnik/swagger/converter/ModelPropertyParser.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ class ModelPropertyParser(cls: Class[_], t: Map[String, String] = Map.empty) (im
206206
else simpleTypeRef
207207
}
208208
simpleName = containerType
209-
if(isComplex(simpleTypeRef)) {
210-
Some(ModelRef(null, Some(simpleTypeRef), Some(basePart)))
209+
if(isComplex(typeRef)) {
210+
Some(ModelRef(null, Some(typeRef), Some(basePart)))
211211
}
212-
else Some(ModelRef(simpleTypeRef, None, Some(basePart)))
212+
else Some(ModelRef(typeRef, None, Some(basePart)))
213213
}
214214
case _ => None
215215
}

modules/swagger-core/src/main/scala/com/wordnik/swagger/model/SwaggerSerializers.scala

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ object SwaggerSerializers extends Serializers {
175175
}
176176
}
177177

178+
def toJsonSchemaTypeFromRef(ref: ModelRef): JObject = {
179+
// see if primitive
180+
if(SwaggerSpec.baseTypes.contains(ref.`type`)) {
181+
toJsonSchema("type", ref.`type`)
182+
} else {
183+
("type" -> ref.`type`)
184+
}
185+
}
186+
178187
class JsonSchemaOperationSerializer extends CustomSerializer[Operation](formats => ({
179188
case json =>
180189
implicit val fmts: Formats = formats
@@ -337,23 +346,22 @@ object SwaggerSerializers extends Serializers {
337346
))
338347

339348
class JsonSchemaModelRefSerializer extends CustomSerializer[ModelRef](formats => ({
340-
case json =>
349+
case json => {
341350
implicit val fmts: Formats = formats
342351
ModelRef(
343352
(json \ "type").extractOrElse(null: String),
344353
(json \ "$ref").extractOpt[String]
345354
)
346-
}, {
347-
case x: ModelRef =>
355+
}}, {
356+
case x: ModelRef => {
348357
implicit val fmts = formats
349-
("type" -> {
350-
x.`type` match {
351-
case e:String => Some(e)
352-
case _ => None
353-
}
354-
}) ~
355-
("$ref" -> x.ref)
358+
val output: JObject = x.`type` match {
359+
case e: String => toJsonSchemaTypeFromRef(x)
360+
case _ => ("type" -> None)
361+
}
362+
output ~ ("$ref" -> x.ref)
356363
}
364+
}
357365
))
358366

359367
class JsonSchemaParameterSerializer extends CustomSerializer[Parameter](formats => ({

modules/swagger-core/src/test/scala/converter/FormatTest.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ class FormatTest extends FlatSpec with Matchers {
3030
val model = ModelConverters.read(classOf[SetModel]).getOrElse(fail("no model found"))
3131
model.properties.size should be (1)
3232

33-
write(model) should be ("""{"id":"SetModel","properties":{"longs":{"type":"array","uniqueItems":true,"items":{"type":"long"}}}}""")
33+
// gbolt; 11/9/2014 - Unclear why the type is expected to be "long"? As far as I understand, long is not one of the
34+
// 7 primitive types defined in JSON Schema. It should be integer with format of int64.
35+
// write(model) should be ("""{"id":"SetModel","properties":{"longs":{"type":"array","uniqueItems":true,"items":{"type":"long"}}}}""")
36+
write(model) should be ("""{"id":"SetModel","properties":{"longs":{"type":"array","uniqueItems":true,"items":{"type":"integer","format":"int64"}}}}""")
37+
3438
}
3539
}
3640

0 commit comments

Comments
 (0)