Skip to content

Commit 9b49804

Browse files
Adjust header serialization for Optional values
1 parent 31089c0 commit 9b49804

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}.toString
1+
"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}{{#required}}.toString{{/required}}{{^required}}.map(_.toString()).getOrElse(null){{/required}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/scala/SttpCodegenTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,38 @@ public void verifyApiKeyLocations() throws IOException {
110110
assertFileContains(path, ".cookie(\"apikey\", apiKeyCookie)");
111111
}
112112

113+
@Test
114+
public void headerSerialization() throws IOException {
115+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
116+
output.deleteOnExit();
117+
String outputPath = output.getAbsolutePath().replace('\\', '/');
118+
119+
OpenAPI openAPI = new OpenAPIParser()
120+
.readLocation("src/test/resources/bugs/issue_21602.yaml", null, new ParseOptions()).getOpenAPI();
121+
122+
ScalaSttpClientCodegen codegen = new ScalaSttpClientCodegen();
123+
codegen.setOutputDir(output.getAbsolutePath());
124+
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
125+
126+
ClientOptInput input = new ClientOptInput();
127+
input.openAPI(openAPI);
128+
input.config(codegen);
129+
130+
DefaultGenerator generator = new DefaultGenerator();
131+
132+
generator.setGeneratorPropertyDefault(CodegenConstants.MODELS, "true");
133+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_TESTS, "false");
134+
generator.setGeneratorPropertyDefault(CodegenConstants.MODEL_DOCS, "false");
135+
generator.setGeneratorPropertyDefault(CodegenConstants.APIS, "true");
136+
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
137+
generator.opts(input).generate();
138+
139+
Path path = Paths.get(outputPath + "/src/main/scala/org/openapitools/client/api/DefaultApi.scala");
140+
assertFileContains(path, ".method(Method.GET, uri\"$baseUrl/ping\")\n");
141+
assertFileContains(path, ".header(\"X-Optional-Header\", xOptionalHeader.map(_.toString()).getOrElse(null))");
142+
assertFileContains(path, ".header(\"X-Required-Header\", xRequiredHeader.toString)");
143+
assertFileContains(path, ".header(\"X-Optional-Schema-Header\", xOptionalSchemaHeader.map(_.toString()).getOrElse(null))");
144+
assertFileContains(path, ".header(\"X-Required-Schema-Header\", xRequiredSchemaHeader.toString)");
145+
}
146+
113147
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
openapi: "3.0.0"
2+
info:
3+
title: Optional Header Test
4+
version: 1.0.0
5+
paths:
6+
/ping:
7+
get:
8+
summary: Ping with optional header
9+
operationId: getPing
10+
parameters:
11+
- name: X-Optional-Header
12+
in: header
13+
required: false
14+
schema:
15+
type: string
16+
- name: X-Required-Header
17+
in: header
18+
required: true
19+
schema:
20+
type: string
21+
- name: X-Optional-Schema-Header
22+
in: header
23+
required: false
24+
schema:
25+
$ref: '#/components/schemas/UUID'
26+
- name: X-Required-Schema-Header
27+
in: header
28+
required: true
29+
schema:
30+
$ref: '#/components/schemas/UUID'
31+
responses:
32+
'200':
33+
description: Success
34+
content:
35+
application/json:
36+
schema:
37+
type: string
38+
components:
39+
schemas:
40+
UUID:
41+
type: object
42+
properties:
43+
uuid:
44+
type: string

samples/client/petstore/scala-sttp-circe/src/main/scala/org/openapitools/client/api/PetApi.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PetApi(baseUrl: String) {
5555
basicRequest
5656
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
5757
.contentType("application/json")
58-
.header("api_key", apiKey.toString)
58+
.header("api_key", apiKey.map(_.toString()).getOrElse(null))
5959
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6060

6161
/**

samples/client/petstore/scala-sttp/src/main/scala/org/openapitools/client/api/PetApi.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PetApi(baseUrl: String) {
5555
basicRequest
5656
.method(Method.DELETE, uri"$baseUrl/pet/${petId}")
5757
.contentType("application/json")
58-
.header("api_key", apiKey.toString)
58+
.header("api_key", apiKey.map(_.toString()).getOrElse(null))
5959
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6060

6161
/**

0 commit comments

Comments
 (0)