diff --git a/modules/openapi-generator/src/main/resources/scala-sttp/paramCreation.mustache b/modules/openapi-generator/src/main/resources/scala-sttp/paramCreation.mustache index 25ec73e8d5e1..a0c90180947b 100644 --- a/modules/openapi-generator/src/main/resources/scala-sttp/paramCreation.mustache +++ b/modules/openapi-generator/src/main/resources/scala-sttp/paramCreation.mustache @@ -1 +1 @@ -"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}.toString \ No newline at end of file +"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}{{#required}}.toString{{/required}}{{^required}}.map(_.toString()){{/required}} \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/SttpCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/SttpCodegenTest.java index 7b745461cf85..c6f2150321bd 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/SttpCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/SttpCodegenTest.java @@ -110,4 +110,42 @@ public void verifyApiKeyLocations() throws IOException { assertFileContains(path, ".cookie(\"apikey\", apiKeyCookie)"); } + @Test + public void headerSerialization() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + String outputPath = output.getAbsolutePath().replace('\\', '/'); + + OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/bugs/issue_21602.yaml", null, new ParseOptions()).getOpenAPI(); + + ScalaSttpClientCodegen codegen = new ScalaSttpClientCodegen(); + codegen.setOutputDir(output.getAbsolutePath()); + codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true"); + + ClientOptInput input = new ClientOptInput(); + input.openAPI(openAPI); + input.config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + + generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false"); + generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false"); + generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true"); + generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false"); + generator.opts(input).generate(); + + Path path = Paths.get(outputPath + "/src/main/scala/org/openapitools/client/api/DefaultApi.scala"); + assertFileContains(path, ".method(Method.GET, uri\"$baseUrl/ping\")\n"); + assertFileContains(path, "xOptionalHeader: Option[String] = None"); + assertFileContains(path, ".header(\"X-Optional-Header\", xOptionalHeader.map(_.toString()))"); + assertFileContains(path, "xRequiredHeader: String"); + assertFileContains(path, ".header(\"X-Required-Header\", xRequiredHeader.toString)"); + assertFileContains(path, "xOptionalSchemaHeader: Option[UUID] = None"); + assertFileContains(path, ".header(\"X-Optional-Schema-Header\", xOptionalSchemaHeader.map(_.toString()))"); + assertFileContains(path, "xRequiredSchemaHeader: UUID"); + assertFileContains(path, ".header(\"X-Required-Schema-Header\", xRequiredSchemaHeader.toString)"); + } + } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_21602.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_21602.yaml new file mode 100644 index 000000000000..928b5603abe2 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_21602.yaml @@ -0,0 +1,44 @@ +openapi: "3.0.0" +info: + title: Optional Header Test + version: 1.0.0 +paths: + /ping: + get: + summary: Ping with optional header + operationId: getPing + parameters: + - name: X-Optional-Header + in: header + required: false + schema: + type: string + - name: X-Required-Header + in: header + required: true + schema: + type: string + - name: X-Optional-Schema-Header + in: header + required: false + schema: + $ref: '#/components/schemas/UUID' + - name: X-Required-Schema-Header + in: header + required: true + schema: + $ref: '#/components/schemas/UUID' + responses: + '200': + description: Success + content: + application/json: + schema: + type: string +components: + schemas: + UUID: + type: object + properties: + uuid: + type: string \ No newline at end of file diff --git a/samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/PetApi.scala b/samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/PetApi.scala index dc45f1f8c72d..663261959b93 100644 --- a/samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/PetApi.scala +++ b/samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/PetApi.scala @@ -55,7 +55,7 @@ class PetApi(baseUrl: String) { basicRequest .method(Method.DELETE, uri"$baseUrl/pet/${petId}") .contentType("application/json") - .header("api_key", apiKey.toString) + .header("api_key", apiKey.map(_.toString())) .response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(())))) /** diff --git a/samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala b/samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala index dc45f1f8c72d..663261959b93 100644 --- a/samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala +++ b/samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala @@ -55,7 +55,7 @@ class PetApi(baseUrl: String) { basicRequest .method(Method.DELETE, uri"$baseUrl/pet/${petId}") .contentType("application/json") - .header("api_key", apiKey.toString) + .header("api_key", apiKey.map(_.toString())) .response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(())))) /**