Skip to content

Commit 02ba9f6

Browse files
[Scala sttp] Fix header serialization for Optional values (#21603)
* Adjust header serialization for Optional values * Add additional test verification to display the header type * Do not get a null value from an option if it is empty, but rather keep the None value. If a value is present it is converted to an Option[String]
1 parent 87231c3 commit 02ba9f6

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-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()){{/required}}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,42 @@ 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, "xOptionalHeader: Option[String] = None");
142+
assertFileContains(path, ".header(\"X-Optional-Header\", xOptionalHeader.map(_.toString()))");
143+
assertFileContains(path, "xRequiredHeader: String");
144+
assertFileContains(path, ".header(\"X-Required-Header\", xRequiredHeader.toString)");
145+
assertFileContains(path, "xOptionalSchemaHeader: Option[UUID] = None");
146+
assertFileContains(path, ".header(\"X-Optional-Schema-Header\", xOptionalSchemaHeader.map(_.toString()))");
147+
assertFileContains(path, "xRequiredSchemaHeader: UUID");
148+
assertFileContains(path, ".header(\"X-Required-Schema-Header\", xRequiredSchemaHeader.toString)");
149+
}
150+
113151
}
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()))
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()))
5959
.response(asString.mapWithMetadata(ResponseAs.deserializeRightWithError(_ => Right(()))))
6060

6161
/**

0 commit comments

Comments
 (0)