Skip to content

Commit beccebd

Browse files
authored
remove stray fields added to Seqs (#49)
1 parent d871c57 commit beccebd

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ object SwaggerScalaModelConverter {
2222
class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
2323
SwaggerScalaModelConverter
2424

25+
private val OptionClass = classOf[scala.Option[_]]
26+
private val IterableClass = classOf[scala.collection.Iterable[_]]
27+
private val SetClass = classOf[scala.collection.Set[_]]
28+
private val BigDecimalClass = classOf[BigDecimal]
29+
private val BigIntClass = classOf[BigInt]
30+
2531
override def resolve(`type`: AnnotatedType, context: ModelConverterContext, chain: Iterator[ModelConverter]): Schema[_] = {
2632
val javaType = _mapper.constructType(`type`.getType)
2733
val cls = javaType.getRawClass
@@ -38,6 +44,13 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
3844
val nextResolved = Option(chain.next().resolve(`type`, context, chain))
3945
nextResolved match {
4046
case Some(property) => {
47+
if (isIterable(cls)) {
48+
//untidy solution for https://github.com/swagger-akka-http/swagger-scala-module/issues/48
49+
property.getRequired.remove("traversableAgain")
50+
val props = property.getProperties
51+
props.remove("traversableAgain")
52+
props.remove("empty")
53+
}
4154
setRequired(`type`)
4255
property
4356
}
@@ -80,11 +93,11 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
8093
}
8194
case _ => {
8295
Option(nullableClass).flatMap { cls =>
83-
if (cls == classOf[BigDecimal]) {
96+
if (cls == BigDecimalClass) {
8497
val dp = PrimitiveType.DECIMAL.createProperty()
8598
setRequired(`type`)
8699
Some(dp)
87-
} else if (cls == classOf[BigInt]) {
100+
} else if (cls == BigIntClass) {
88101
val ip = PrimitiveType.INT.createProperty()
89102
setRequired(`type`)
90103
Some(ip)
@@ -130,7 +143,7 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
130143

131144
override def _isSetType(cls: Class[_]): Boolean = {
132145
val setInterfaces = cls.getInterfaces.find { interface =>
133-
interface == classOf[scala.collection.Set[_]]
146+
interface == SetClass
134147
}
135148
setInterfaces.isDefined || super._isSetType(cls)
136149
}
@@ -163,7 +176,8 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) {
163176
else None
164177
}
165178

166-
private def isOption(cls: Class[_]): Boolean = cls == classOf[scala.Option[_]]
179+
private def isOption(cls: Class[_]): Boolean = cls == OptionClass
180+
private def isIterable(cls: Class[_]): Boolean = IterableClass.isAssignableFrom(cls)
167181

168182
private def nullSafeList[T](array: Array[T]): List[T] = Option(array) match {
169183
case None => List.empty[T]

src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
259259
val arraySchema = stringsField.asInstanceOf[ArraySchema]
260260
arraySchema.getUniqueItems() shouldBe (null)
261261
arraySchema.getItems shouldBe a [StringSchema]
262+
nullSafeMap(arraySchema.getProperties()) shouldBe empty
263+
nullSafeList(arraySchema.getRequired()) shouldBe empty
262264
}
263265

264266
it should "process Model with Scala Set" in {
@@ -272,6 +274,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
272274
val arraySchema = stringsField.asInstanceOf[ArraySchema]
273275
arraySchema.getUniqueItems() shouldBe true
274276
arraySchema.getItems shouldBe a [StringSchema]
277+
nullSafeMap(arraySchema.getProperties()) shouldBe empty
278+
nullSafeList(arraySchema.getRequired()) shouldBe empty
275279
}
276280

277281
it should "process Model with Java List" in {
@@ -285,9 +289,11 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
285289
val arraySchema = stringsField.asInstanceOf[ArraySchema]
286290
arraySchema.getUniqueItems() shouldBe (null)
287291
arraySchema.getItems shouldBe a [StringSchema]
292+
nullSafeMap(arraySchema.getProperties()) shouldBe empty
293+
nullSafeList(arraySchema.getRequired()) shouldBe empty
288294
}
289295

290-
def findModel(schemas: Map[String, Schema[_]], name: String): Option[Schema[_]] = {
296+
private def findModel(schemas: Map[String, Schema[_]], name: String): Option[Schema[_]] = {
291297
schemas.get(name) match {
292298
case Some(m) => Some(m)
293299
case None =>
@@ -298,8 +304,13 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue
298304
}
299305
}
300306

301-
def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match {
307+
private def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match {
302308
case None => List[T]()
303309
case Some(l) => l.asScala.toList
304310
}
311+
312+
private def nullSafeMap[K, V](map: java.util.Map[K, V]): Map[K, V] = Option(map) match {
313+
case None => Map[K, V]()
314+
case Some(m) => m.asScala.toMap
315+
}
305316
}

0 commit comments

Comments
 (0)