From 687a78a0cc7fc9daa9697720cf205dcde05a452d Mon Sep 17 00:00:00 2001 From: Greg McKay Date: Tue, 18 Jun 2024 10:36:21 +0100 Subject: [PATCH 1/4] codgegen: Optional query parameters --- .../main/scala/sttp/tapir/codegen/EndpointGenerator.scala | 4 +++- .../scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) 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..1ba9f943aa 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,10 @@ class EndpointGenerator { param.schema match { case st: OpenapiSchemaSimpleType => val (t, _) = mapSchemaSimpleTypeToType(st) + val required = (param.in != "query") || param.param.required.getOrElse(true) + val req = if (required) 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..de20ec5c49 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,9 @@ 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)))), responses = Seq( OpenapiResponse( "200", @@ -60,6 +62,7 @@ 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 shouldCompile () } From 950cc948c98013bcdc877d4d730535a5bad9d4e0 Mon Sep 17 00:00:00 2001 From: Greg McKay Date: Tue, 18 Jun 2024 11:24:27 +0100 Subject: [PATCH 2/4] fix --- .../src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1ba9f943aa..7c60cabacc 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,7 +182,7 @@ class EndpointGenerator { param.schema match { case st: OpenapiSchemaSimpleType => val (t, _) = mapSchemaSimpleTypeToType(st) - val required = (param.in != "query") || param.param.required.getOrElse(true) + val required = (param.in != "query") || param.required.getOrElse(true) val req = if (required) t else s"Option[$t]" val desc = param.description.map(d => JavaEscape.escapeString(d)).fold("")(d => s""".description("$d")""") s""".in(${param.in}[$req]("${param.name}")$desc)""" From 27391450ce1a539723861d62624eabbb58d7e1f5 Mon Sep 17 00:00:00 2001 From: Greg McKay Date: Tue, 18 Jun 2024 11:27:54 +0100 Subject: [PATCH 3/4] apply to header too --- .../src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala | 3 +-- .../test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) 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 7c60cabacc..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,7 @@ class EndpointGenerator { param.schema match { case st: OpenapiSchemaSimpleType => val (t, _) = mapSchemaSimpleTypeToType(st) - val required = (param.in != "query") || param.required.getOrElse(true) - val req = if (required) t else s"Option[$t]" + 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}[$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 de20ec5c49..1b089ba68f 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 @@ -42,6 +42,7 @@ class EndpointGeneratorSpec extends CompileCheckTestBase { 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", @@ -63,6 +64,7 @@ class EndpointGeneratorSpec extends CompileCheckTestBase { 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 () } From 7cf8bdea01c47d26d764f35d9640af1d0c579013 Mon Sep 17 00:00:00 2001 From: Greg McKay Date: Tue, 18 Jun 2024 11:41:24 +0100 Subject: [PATCH 4/4] fix --- .../test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1b089ba68f..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 @@ -41,7 +41,7 @@ class EndpointGeneratorSpec extends CompileCheckTestBase { methodType = "get", parameters = Seq( Resolved(OpenapiParameter("asd-id", "path", Some(true), None, OpenapiSchemaString(false))), - Resolved(OpenapiParameter("fgh-id", "query", Some(false), 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(