Skip to content

Commit

Permalink
Type.Name is not suitable for parameterized types
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Stewart authored and blast-hardcheese committed Jul 31, 2018
1 parent c9a7b04 commit 1629ce6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 25 additions & 5 deletions src/test/scala/tests/core/TypesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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 _)
}
"""

Expand Down

0 comments on commit 1629ce6

Please sign in to comment.