From 1629ce6d88b241aee52a8b29c8d3fae343570b38 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Thu, 5 Jul 2018 20:38:30 -0700 Subject: [PATCH] Type.Name is not suitable for parameterized types --- .../com/twilio/guardrail/SwaggerUtil.scala | 17 ++++++----- src/test/scala/tests/core/TypesTest.scala | 30 +++++++++++++++---- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/modules/codegen/src/main/scala/com/twilio/guardrail/SwaggerUtil.scala b/modules/codegen/src/main/scala/com/twilio/guardrail/SwaggerUtil.scala index 9876fa154a..6d8433e983 100644 --- a/modules/codegen/src/main/scala/com/twilio/guardrail/SwaggerUtil.scala +++ b/modules/codegen/src/main/scala/com/twilio/guardrail/SwaggerUtil.scala @@ -222,14 +222,15 @@ object SwaggerUtil { t } def liftCustomType(s: String): Option[Type] = { - val terms = s.split('.').toList - val (init, last) = (terms.init, terms.last) - init.map(Term.Name.apply _) match { - case Nil if last == "" => None - case Nil => Some(Type.Name(last)) - case rest => - Some(Type.Select(rest.reduceLeft(Term.Select.apply _), Type.Name(last))) - } + val tpe = s.trim + if (tpe.nonEmpty) { + tpe + .parse[Type] + .fold({ err => + println(s"Warning: Unparsable x-scala-type: ${tpe} ${err}") + None + }, Option.apply _) + } else None } customType.flatMap(liftCustomType _).getOrElse { diff --git a/src/test/scala/tests/core/TypesTest.scala b/src/test/scala/tests/core/TypesTest.scala index 94d80611d9..e27ce63966 100644 --- a/src/test/scala/tests/core/TypesTest.scala +++ b/src/test/scala/tests/core/TypesTest.scala @@ -58,6 +58,12 @@ class TypesTest extends FunSuite with Matchers with SwaggerSpecRunner { | type: integer | untyped: | description: Untyped + | custom: + | type: string + | x-scala-type: Foo + | customComplex: + | type: string + | x-scala-type: Foo[Bar] |""".stripMargin test("Generate no definitions") { @@ -68,18 +74,32 @@ class TypesTest extends FunSuite with Matchers with SwaggerSpecRunner { ) = runSwaggerSpec(swagger)(Context.empty, AkkaHttp, defaults.akkaGeneratorSettings) val definition = q""" - case class Types(array: Option[IndexedSeq[Boolean]] = Option(IndexedSeq.empty), obj: Option[io.circe.Json] = None, bool: Option[Boolean] = None, string: Option[String] = None, date: Option[java.time.LocalDate] = None, dateTime: Option[java.time.OffsetDateTime] = None, long: Option[Long] = None, int: Option[Int] = None, float: Option[Float] = None, double: Option[Double] = None, number: Option[BigDecimal] = None, integer: Option[BigInt] = None, untyped: Option[io.circe.Json] = None) + case class Types( + array: Option[IndexedSeq[Boolean]] = Option(IndexedSeq.empty), + obj: Option[io.circe.Json] = None, + bool: Option[Boolean] = None, + string: Option[String] = None, + date: Option[java.time.LocalDate] = None, + date_time: Option[java.time.OffsetDateTime] = None, + long: Option[Long] = None, + int: Option[Int] = None, + float: Option[Float] = None, + double: Option[Double] = None, + number: Option[BigDecimal] = None, + integer: Option[BigInt] = None, + untyped: Option[io.circe.Json] = None, + custom: Option[Foo] = None, + customComplex: Option[Foo[Bar]] = None + ) """ val companion = q""" object Types { implicit val encodeTypes = { val readOnlyKeys = Set[String]() - Encoder.forProduct13("array", "obj", "bool", "string", "date", "date_time", "long", "int", "float", "double", "number", "integer", "untyped")( (o: Types) => - (o.array, o.obj, o.bool, o.string, o.date, o.dateTime, o.long, o.int, o.float, o.double, o.number, o.integer, o.untyped) - ).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key))) + Encoder.forProduct15("array", "obj", "bool", "string", "date", "date_time", "long", "int", "float", "double", "number", "integer", "untyped", "custom", "customComplex")((o: Types) => (o.array, o.obj, o.bool, o.string, o.date, o.date_time, o.long, o.int, o.float, o.double, o.number, o.integer, o.untyped, o.custom, o.customComplex)).mapJsonObject(_.filterKeys(key => !(readOnlyKeys contains key))) } - implicit val decodeTypes = Decoder.forProduct13("array", "obj", "bool", "string", "date", "date_time", "long", "int", "float", "double", "number", "integer", "untyped")(Types.apply _) + implicit val decodeTypes = Decoder.forProduct15("array", "obj", "bool", "string", "date", "date_time", "long", "int", "float", "double", "number", "integer", "untyped", "custom", "customComplex")(Types.apply _) } """