Skip to content

Commit 8d99779

Browse files
committed
[kotlin] Add = null default for optional parameters in jvm-ktor templates and javalin serviceImpls
Expanded test coverage for jvm-ktor optional parameters to verify `= null` defaults in generated code. Updated javalin5 and javalin6 serviceImpl templates to include correct default value assignments.
1 parent 4a5ec7c commit 8d99779

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin5/serviceImpl.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.Flow
1010
class {{classname}}ServiceImpl : {{classname}}Service {
1111
{{#operation}}
1212

13-
override {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{paramName}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{^required}}{{^defaultValue}} = null{{/defaultValue}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): {{>returnTypes}} {
13+
override {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{paramName}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{^-last}}, {{/-last}}{{/allParams}}): {{>returnTypes}} {
1414
TODO("Implement me")
1515
}
1616
{{/operation}}

modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/serviceImpl.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlinx.coroutines.flow.Flow
1111
class {{classname}}ServiceImpl : {{classname}}Service {
1212
{{#operation}}
1313

14-
override {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{paramName}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{^required}}{{^defaultValue}} = null{{/defaultValue}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}ctx: Context): {{>returnTypes}} {
14+
override {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{paramName}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{/isArray}}{{/reactive}}{{/isBodyParam}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}ctx: Context): {{>returnTypes}} {
1515
TODO("Implement me")
1616
}
1717
{{/operation}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ public void testUseResponseAsReturnType(Object useResponseAsReturnType, String e
9191
assertFileContainsLine(lines, "suspend fun deletePet(@Path(\"petId\") petId: kotlin.Long, @Header(\"api_key\") apiKey: kotlin.String? = null)" + expectedUnitResponse);
9292
}
9393

94+
@Test
95+
public void testOptionalParamsHaveDefaultNullJvmKtor() throws IOException {
96+
OpenAPI openAPI = readOpenAPI("3_0/kotlin/petstore.yaml");
97+
98+
KotlinClientCodegen codegen = createCodegen(ClientLibrary.JVM_KTOR);
99+
100+
ClientOptInput input = createClientOptInput(openAPI, codegen);
101+
102+
DefaultGenerator generator = new DefaultGenerator();
103+
enableOnlyApiGeneration(generator);
104+
105+
List<File> files = generator.opts(input).generate();
106+
File petApi = files.stream().filter(file -> file.getName().equals("PetApi.kt")).findAny().orElseThrow();
107+
108+
assertFileContains(petApi.toPath(), "apiKey: kotlin.String? = null");
109+
}
110+
94111
@Test
95112
public void testEnumDefaultForReferencedSchemaParameterJvmOkhttp4() throws IOException {
96113
OpenAPI openAPI = readOpenAPI("3_0/kotlin/enum-default-query.yaml");

0 commit comments

Comments
 (0)