From 9bb35ac2604cec2042711331635c0fc2233a7fb2 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Mon, 8 Aug 2022 10:58:39 +0100 Subject: [PATCH] handle value types like Option[Seq[T]] (#174) * handle value types like Option[Seq[T]] * Update ErasureHelperTest.scala --- .../github/swagger/scala/converter/ErasureHelper.scala | 9 +++++++-- .../swagger/scala/converter/ErasureHelperTest.scala | 8 ++++++++ .../github/swagger/scala/converter/OptionSeqLong.scala | 3 +++ .../github/swagger/scala/converter/SeqOptionLong.scala | 3 +++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/test/scala/com/github/swagger/scala/converter/OptionSeqLong.scala create mode 100644 src/test/scala/com/github/swagger/scala/converter/SeqOptionLong.scala diff --git a/src/main/scala-2/com/github/swagger/scala/converter/ErasureHelper.scala b/src/main/scala-2/com/github/swagger/scala/converter/ErasureHelper.scala index a9246f7..12cc6c4 100644 --- a/src/main/scala-2/com/github/swagger/scala/converter/ErasureHelper.scala +++ b/src/main/scala-2/com/github/swagger/scala/converter/ErasureHelper.scala @@ -26,8 +26,13 @@ private[converter] object ErasureHelper { properties.flatMap { prop: universe.Symbol => val maybeClass: Option[Class[_]] = prop.typeSignature.typeArgs.headOption.flatMap { signature => if (signature.typeSymbol.isClass) { - Option(mirror.runtimeClass(signature.typeSymbol.asClass)) - } else None + signature.typeArgs.headOption match { + case Some(typeArg) => Option(mirror.runtimeClass(typeArg)) + case _ => Option(mirror.runtimeClass(signature)) + } + } else { + None + } } maybeClass.map(prop.name.toString.trim -> _) }.toMap diff --git a/src/test/scala/com/github/swagger/scala/converter/ErasureHelperTest.scala b/src/test/scala/com/github/swagger/scala/converter/ErasureHelperTest.scala index b4d9560..2d773cb 100644 --- a/src/test/scala/com/github/swagger/scala/converter/ErasureHelperTest.scala +++ b/src/test/scala/com/github/swagger/scala/converter/ErasureHelperTest.scala @@ -13,4 +13,12 @@ class ErasureHelperTest extends AnyFlatSpec with Matchers { "ErasureHelper" should "handle MyTrait" in { ErasureHelper.erasedOptionalPrimitives(classOf[ErasureHelperTest.SuperType]) shouldBe empty } + it should "handle OptionSeqLong" in { + val expected = if (RuntimeUtil.isScala3()) Map.empty[String, Class[_]] else Map("values" -> classOf[Long]) + ErasureHelper.erasedOptionalPrimitives(classOf[OptionSeqLong]) shouldBe expected + } + it should "handle SeqOptionLong" in { + val expected = if (RuntimeUtil.isScala3()) Map.empty[String, Class[_]] else Map("values" -> classOf[Long]) + ErasureHelper.erasedOptionalPrimitives(classOf[SeqOptionLong]) shouldBe expected + } } diff --git a/src/test/scala/com/github/swagger/scala/converter/OptionSeqLong.scala b/src/test/scala/com/github/swagger/scala/converter/OptionSeqLong.scala new file mode 100644 index 0000000..ce3e4e1 --- /dev/null +++ b/src/test/scala/com/github/swagger/scala/converter/OptionSeqLong.scala @@ -0,0 +1,3 @@ +package com.github.swagger.scala.converter + +case class OptionSeqLong(values: Option[Seq[Long]]) diff --git a/src/test/scala/com/github/swagger/scala/converter/SeqOptionLong.scala b/src/test/scala/com/github/swagger/scala/converter/SeqOptionLong.scala new file mode 100644 index 0000000..4c4d132 --- /dev/null +++ b/src/test/scala/com/github/swagger/scala/converter/SeqOptionLong.scala @@ -0,0 +1,3 @@ +package com.github.swagger.scala.converter + +case class SeqOptionLong(values: Seq[Option[Long]])