Skip to content

[Scala sttp] Fix header serialization for Optional values #21603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}.toString
"{{baseName}}", {{#isContainer}}ArrayValues({{{paramName}}}{{#collectionFormat}}, {{collectionFormat.toUpperCase}}{{/collectionFormat}}){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}}{{#required}}.toString{{/required}}{{^required}}.map(_.toString()){{/required}}
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}

}
44 changes: 44 additions & 0 deletions modules/openapi-generator/src/test/resources/bugs/issue_21602.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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(()))))

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(()))))

/**
Expand Down
Loading