Skip to content

Commit

Permalink
Fix optional AsSingleOpenApiParam
Browse files Browse the repository at this point in the history
  • Loading branch information
ulanzetz committed Jan 22, 2021
1 parent 4f0d49a commit 895a06e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.tags
.attach_pid*
metals.sbt
.vscode
.vscode
.bsp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ object AsOpenApiParam extends AsOpenParamInstances[AsOpenApiParam] with Derivati

def generate[T]: Typeclass[T] = macro Magnolia.gen[T]
def instance[T]: Typeclass[T] = macro Magnolia.gen[T]

implicit def optParam[T](implicit param: AsOpenApiParam[T]): AsOpenApiParam[Option[T]] =
param.optional
}

trait OpenApiParamInfo {
Expand All @@ -46,10 +49,12 @@ final case class AsSingleOpenApiParam[T](typ: SwaggerType, required: Boolean = t
def optional: AsOpenApiParam[Option[T]] = AsSingleOpenApiParam(typ, required = false)
}

object AsSingleOpenApiParam extends AsOpenParamInstances[AsSingleOpenApiParam]
object AsSingleOpenApiParam extends AsOpenParamInstances[AsSingleOpenApiParam] {
final implicit def optSingleParam[T](implicit typ: SwaggerTypeable[T]): AsSingleOpenApiParam[Option[T]] =
AsSingleOpenApiParam[Option[T]](typ = typ.typ, required = false)
}

trait AsOpenParamInstances[TC[x] >: AsSingleOpenApiParam[x]] {
final implicit def requiredParam[T](implicit typ: SwaggerTypeable[T]): TC[T] =
final implicit def requiredSingleParam[T](implicit typ: SwaggerTypeable[T]): TC[T] =
AsSingleOpenApiParam[T](typ = typ.typ, required = true)
final implicit def optParam[T](implicit param: AsOpenApiParam[T]): AsOpenApiParam[Option[T]] = param.optional
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,18 @@ class MkSwaggerSpec extends AnyFlatSpec {

assert((foo ++ bar).describe(description).make(OpenApiInfo()).tags == output)
}

"optional header" should "has required = false in OpenAPI" in {
val swagger = MkSwagger(
(
tag("tag") :>
get :>
operation("operation") :>
header[Option[String]]("X-Kek-Id") :>
$$[String]
)
)

assert(!swagger.paths.head.op.parameters.find(_.name == "X-Kek-Id").get.required)
}
}

0 comments on commit 895a06e

Please sign in to comment.