Skip to content

Commit

Permalink
Merge pull request #259 from http4s/issue/257-optional-list-param
Browse files Browse the repository at this point in the history
 Fix issue #257
  • Loading branch information
zarthross authored Oct 11, 2018
2 parents d49234f + 26f9651 commit 3772201
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private[swagger] class SwaggerModelsBuilder(formats: SwaggerFormats) {
case _ : TypeBuilder.DataType.ComplexDataType =>
tpe :: go(x::xs)
case TypeBuilder.DataType.ContainerDataType(_, Some(_: TypeBuilder.DataType.ComplexDataType), _) =>
q.m.tpe.typeArgs.head :: go(x::xs)
q.m.tpe.dealias.typeArgs.head :: go(x::xs)
case _ => go(x::xs)
}

Expand Down Expand Up @@ -346,7 +346,8 @@ private[swagger] class SwaggerModelsBuilder(formats: SwaggerFormats) {
def mkQueryParam[F[_]](rule: QueryMetaData[F, _]): Parameter = {
val required = !(rule.m.tpe.isOption || rule.default.isDefined)

TypeBuilder.DataType(rule.m.tpe) match {
val tpe = if(rule.m.tpe.isOption) rule.m.tpe.dealias.typeArgs.head else rule.m.tpe
TypeBuilder.DataType(tpe) match {
case TypeBuilder.DataType.ComplexDataType(nm, _) =>
QueryParameter(
`type` = nm.some,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SwaggerModelsBuilderSpec extends Specification {
val sb = new SwaggerModelsBuilder(DefaultSwaggerFormats)
val fooPath = GET / "foo"
val barPath = GET / "bar"
type OpSeq = Option[Seq[String]]

"SwaggerModelsBuilder.collectQueryParams" should {

Expand Down Expand Up @@ -88,6 +89,28 @@ class SwaggerModelsBuilderSpec extends Specification {
List(QueryParameter(`type` = "string".some, name = "name".some, required = false))
}

"handle an action with one optional seq query parameter" in {
val ra = fooPath +? param[Option[Seq[String]]]("name") |>> { (s: Option[Seq[String]]) => "" }

sb.collectQueryParams[IO](ra) must_==
List(
QueryParameter(`type` = None, name = "name".some,
items = Some(AbstractProperty(`type` = "string")),
defaultValue = None, isArray = true, required = false)
)
}

"handle an action with one optional seq query parameter using a type alias" in {
val ra = fooPath +? param[OpSeq]("name") |>> { (s: OpSeq) => "" }

sb.collectQueryParams[IO](ra) must_==
List(
QueryParameter(`type` = None, name = "name".some,
items = Some(AbstractProperty(`type` = "string")),
defaultValue = None, isArray = true, required = false)
)
}

"handle an action with one query parameter with default value" in {
val ra = fooPath +? param[Int]("id", 6) |>> { (i: Int) => "" }

Expand Down

0 comments on commit 3772201

Please sign in to comment.