Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 =>
Expand All @@ -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
}
}