diff --git a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala index 84adf5cc13..69df3a0f83 100644 --- a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala +++ b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala @@ -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") } } diff --git a/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala b/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala index a0cdfd8588..68e42db635 100644 --- a/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala +++ b/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala @@ -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", @@ -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 () }