From 8c28bc90329411e2234f85cfe32ff246b5df845d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 7 Jun 2020 11:58:40 +0200 Subject: [PATCH] remove stray fields added to Seqs --- .../SwaggerScalaModelConverter.scala | 22 +++++++++++++++---- .../converter/ModelPropertyParserTest.scala | 15 +++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala index fdfe0d04..9d5c4756 100644 --- a/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala +++ b/src/main/scala/com/github/swagger/scala/converter/SwaggerScalaModelConverter.scala @@ -22,6 +22,12 @@ object SwaggerScalaModelConverter { class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { SwaggerScalaModelConverter + private val OptionClass = classOf[scala.Option[_]] + private val IterableClass = classOf[scala.collection.Iterable[_]] + private val SetClass = classOf[scala.collection.Set[_]] + private val BigDecimalClass = classOf[BigDecimal] + private val BigIntClass = classOf[BigInt] + override def resolve(`type`: AnnotatedType, context: ModelConverterContext, chain: Iterator[ModelConverter]): Schema[_] = { val javaType = _mapper.constructType(`type`.getType) val cls = javaType.getRawClass @@ -38,6 +44,13 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { val nextResolved = Option(chain.next().resolve(`type`, context, chain)) nextResolved match { case Some(property) => { + if (isIterable(cls)) { + //untidy solution for https://github.com/swagger-akka-http/swagger-scala-module/issues/48 + property.getRequired.remove("traversableAgain") + val props = property.getProperties + props.remove("traversableAgain") + props.remove("empty") + } setRequired(`type`) property } @@ -80,11 +93,11 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { } case _ => { Option(nullableClass).flatMap { cls => - if (cls == classOf[BigDecimal]) { + if (cls == BigDecimalClass) { val dp = PrimitiveType.DECIMAL.createProperty() setRequired(`type`) Some(dp) - } else if (cls == classOf[BigInt]) { + } else if (cls == BigIntClass) { val ip = PrimitiveType.INT.createProperty() setRequired(`type`) Some(ip) @@ -130,7 +143,7 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { override def _isSetType(cls: Class[_]): Boolean = { val setInterfaces = cls.getInterfaces.find { interface => - interface == classOf[scala.collection.Set[_]] + interface == SetClass } setInterfaces.isDefined || super._isSetType(cls) } @@ -163,7 +176,8 @@ class SwaggerScalaModelConverter extends ModelResolver(Json.mapper()) { else None } - private def isOption(cls: Class[_]): Boolean = cls == classOf[scala.Option[_]] + private def isOption(cls: Class[_]): Boolean = cls == OptionClass + private def isIterable(cls: Class[_]): Boolean = IterableClass.isAssignableFrom(cls) private def nullSafeList[T](array: Array[T]): List[T] = Option(array) match { case None => List.empty[T] diff --git a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala index 99be29d3..183119d3 100644 --- a/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala +++ b/src/test/scala/com/github/swagger/scala/converter/ModelPropertyParserTest.scala @@ -259,6 +259,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue val arraySchema = stringsField.asInstanceOf[ArraySchema] arraySchema.getUniqueItems() shouldBe (null) arraySchema.getItems shouldBe a [StringSchema] + nullSafeMap(arraySchema.getProperties()) shouldBe empty + nullSafeList(arraySchema.getRequired()) shouldBe empty } it should "process Model with Scala Set" in { @@ -272,6 +274,8 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue val arraySchema = stringsField.asInstanceOf[ArraySchema] arraySchema.getUniqueItems() shouldBe true arraySchema.getItems shouldBe a [StringSchema] + nullSafeMap(arraySchema.getProperties()) shouldBe empty + nullSafeList(arraySchema.getRequired()) shouldBe empty } it should "process Model with Java List" in { @@ -285,9 +289,11 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue val arraySchema = stringsField.asInstanceOf[ArraySchema] arraySchema.getUniqueItems() shouldBe (null) arraySchema.getItems shouldBe a [StringSchema] + nullSafeMap(arraySchema.getProperties()) shouldBe empty + nullSafeList(arraySchema.getRequired()) shouldBe empty } - def findModel(schemas: Map[String, Schema[_]], name: String): Option[Schema[_]] = { + private def findModel(schemas: Map[String, Schema[_]], name: String): Option[Schema[_]] = { schemas.get(name) match { case Some(m) => Some(m) case None => @@ -298,8 +304,13 @@ class ModelPropertyParserTest extends AnyFlatSpec with Matchers with OptionValue } } - def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match { + private def nullSafeList[T](list: java.util.List[T]): List[T] = Option(list) match { case None => List[T]() case Some(l) => l.asScala.toList } + + private def nullSafeMap[K, V](map: java.util.Map[K, V]): Map[K, V] = Option(map) match { + case None => Map[K, V]() + case Some(m) => m.asScala.toMap + } }