Skip to content

Commit

Permalink
codgegen: Optional query parameters (#3855)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrGreggles authored Jun 18, 2024
1 parent 1e064e3 commit c8316ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ class EndpointGenerator {
param.schema match {
case st: OpenapiSchemaSimpleType =>
val (t, _) = mapSchemaSimpleTypeToType(st)
val req = if (param.required.getOrElse(true)) t else s"Option[$t]"
val desc = param.description.map(d => JavaEscape.escapeString(d)).fold("")(d => s""".description("$d")""")
s""".in(${param.in}[$t]("${param.name}")$desc)"""
s""".in(${param.in}[$req]("${param.name}")$desc)"""
case x => bail(s"Can't create non-simple params to input - found $x")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class EndpointGeneratorSpec extends CompileCheckTestBase {
Seq(
OpenapiPathMethod(
methodType = "get",
parameters = Seq(Resolved(OpenapiParameter("asd-id", "path", Some(true), None, OpenapiSchemaString(false)))),
parameters = Seq(
Resolved(OpenapiParameter("asd-id", "path", Some(true), None, OpenapiSchemaString(false))),
Resolved(OpenapiParameter("fgh-id", "query", Some(false), None, OpenapiSchemaString(false))),
Resolved(OpenapiParameter("jkl-id", "header", Some(false), None, OpenapiSchemaString(false)))),
responses = Seq(
OpenapiResponse(
"200",
Expand All @@ -60,6 +63,8 @@ class EndpointGeneratorSpec extends CompileCheckTestBase {
val generatedCode = BasicGenerator.imports(JsonSerdeLib.Circe) ++
new EndpointGenerator().endpointDefs(doc, useHeadTagForObjectNames = false).endpointDecls(None)
generatedCode should include("val getTestAsdId =")
generatedCode should include(""".in(query[Option[String]]("fgh-id"))""")
generatedCode should include(""".in(header[Option[String]]("jkl-id"))""")
generatedCode shouldCompile ()
}

Expand Down

0 comments on commit c8316ee

Please sign in to comment.