diff --git a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache index 03efece88e47..8ef38bb9f2e4 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache @@ -52,7 +52,7 @@ import com.fasterxml.jackson.databind.ObjectMapper {{#returnType}} @Suppress("UNCHECKED_CAST") {{/returnType}} - {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): HttpResponse<{{{returnType}}}{{^returnType}}Unit{{/returnType}}> { + {{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}}{{^required}} = null{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allParams}}): HttpResponse<{{{returnType}}}{{^returnType}}Unit{{/returnType}}> { val localVariableAuthNames = listOf({{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}}) diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin5/service.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin5/service.mustache index 73fb1332fda7..52d19653b5bd 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin5/service.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin5/service.mustache @@ -27,7 +27,7 @@ interface {{classname}}Service { {{/externalDocs}} * @see {{classname}}#{{operationId}} */ - {{#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}} + {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{^required}}{{^defaultValue}}?{{/defaultValue}}{{/required}}{{/isArray}}{{/reactive}}{{/isBodyParam}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}}{{^required}} = null{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allParams}}): {{>returnTypes}} {{/operation}} } {{/operations}} diff --git a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/service.mustache b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/service.mustache index 0c524ab1ec6a..bb510bcd3367 100644 --- a/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/service.mustache +++ b/modules/openapi-generator/src/main/resources/kotlin-server/libraries/javalin6/service.mustache @@ -29,7 +29,7 @@ interface {{classname}}Service { {{/externalDocs}} * @see {{classname}}#{{operationId}} */ - {{#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}} + {{#reactive}}{{^isArray}}suspend {{/isArray}}{{/reactive}}fun {{operationId}}({{#allParams}}{{{paramName}}}: {{^isBodyParam}}{{>optionalDataType}}{{/isBodyParam}}{{#isBodyParam}}{{^reactive}}{{>optionalDataType}}{{/reactive}}{{#reactive}}{{^isArray}}{{>optionalDataType}}{{/isArray}}{{#isArray}}Flow<{{{baseType}}}>{{^required}}{{^defaultValue}}?{{/defaultValue}}{{/required}}{{/isArray}}{{/reactive}}{{/isBodyParam}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}}{{^required}} = null{{/required}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/allParams}}{{#hasParams}}, {{/hasParams}}ctx: Context): {{>returnTypes}} {{/operation}} } {{/operations}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java index 69e1b3ecd2bd..538891a21241 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/kotlin/KotlinClientCodegenApiTest.java @@ -91,6 +91,23 @@ public void testUseResponseAsReturnType(Object useResponseAsReturnType, String e assertFileContainsLine(lines, "suspend fun deletePet(@Path(\"petId\") petId: kotlin.Long, @Header(\"api_key\") apiKey: kotlin.String? = null)" + expectedUnitResponse); } + @Test + public void testOptionalParamsHaveDefaultNullJvmKtor() throws IOException { + OpenAPI openAPI = readOpenAPI("3_0/kotlin/petstore.yaml"); + + KotlinClientCodegen codegen = createCodegen(ClientLibrary.JVM_KTOR); + + ClientOptInput input = createClientOptInput(openAPI, codegen); + + DefaultGenerator generator = new DefaultGenerator(); + enableOnlyApiGeneration(generator); + + List files = generator.opts(input).generate(); + File petApi = files.stream().filter(file -> file.getName().equals("PetApi.kt")).findAny().orElseThrow(); + + assertFileContains(petApi.toPath(), "apiKey: kotlin.String? = null"); + } + @Test public void testEnumDefaultForReferencedSchemaParameterJvmOkhttp4() throws IOException { OpenAPI openAPI = readOpenAPI("3_0/kotlin/enum-default-query.yaml"); diff --git a/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt b/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt index bc806e41e6d0..5c52462937f4 100644 --- a/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt +++ b/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt @@ -80,7 +80,7 @@ import java.text.DateFormat * @param status2 number type (optional) * @return void */ - open suspend fun updatePetWithFormNumber(petId: kotlin.Long, name: kotlin.String?, status: kotlin.Int?, status2: java.math.BigDecimal?): HttpResponse { + open suspend fun updatePetWithFormNumber(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.Int? = null, status2: java.math.BigDecimal? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") diff --git a/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 10be7a7bdf01..b3d9ee1b3130 100644 --- a/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -80,7 +80,7 @@ import java.text.DateFormat * @param apiKey (optional) * @return void */ - open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse { + open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -253,7 +253,7 @@ import java.text.DateFormat * @param status Updated status of the pet (optional) * @return void */ - open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse { + open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -292,7 +292,7 @@ import java.text.DateFormat * @return ModelApiResponse */ @Suppress("UNCHECKED_CAST") - open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart?): HttpResponse { + open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") diff --git a/samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index d81dff91aa77..125b891deefb 100644 --- a/samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -77,7 +77,7 @@ import com.fasterxml.jackson.databind.ObjectMapper * @param apiKey (optional) * @return void */ - open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse { + open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -249,7 +249,7 @@ import com.fasterxml.jackson.databind.ObjectMapper * @param status Updated status of the pet (optional) * @return void */ - open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse { + open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -288,7 +288,7 @@ import com.fasterxml.jackson.databind.ObjectMapper * @return ModelApiResponse */ @Suppress("UNCHECKED_CAST") - open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart?): HttpResponse { + open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") diff --git a/samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/apis/PetApi.kt b/samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/apis/PetApi.kt index 9b1780135f25..60110a0ec8d4 100644 --- a/samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/apis/PetApi.kt +++ b/samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/apis/PetApi.kt @@ -75,7 +75,7 @@ import io.ktor.http.ParametersBuilder * @param apiKey (optional) * @return void */ - open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse { + open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -248,7 +248,7 @@ import io.ktor.http.ParametersBuilder * @param status Updated status of the pet (optional) * @return void */ - open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse { + open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") @@ -287,7 +287,7 @@ import io.ktor.http.ParametersBuilder * @return ModelApiResponse */ @Suppress("UNCHECKED_CAST") - open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart?): HttpResponse { + open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart? = null): HttpResponse { val localVariableAuthNames = listOf("petstore_auth") diff --git a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt index 6c06f129bead..29534749b1ec 100644 --- a/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt +++ b/samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt @@ -28,7 +28,7 @@ interface PetApiService { * @return Invalid pet value (status code 400) * @see PetApi#deletePet */ - fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?, ctx: Context): Unit + fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null, ctx: Context): Unit /** * GET /pet/findByStatus : Finds Pets by status @@ -95,7 +95,7 @@ interface PetApiService { * @return Invalid input (status code 405) * @see PetApi#updatePetWithForm */ - fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?, ctx: Context): Unit + fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null, ctx: Context): Unit /** * POST /pet/{petId}/uploadImage : uploads an image @@ -108,5 +108,5 @@ interface PetApiService { * @return successful operation (status code 200) * @see PetApi#uploadFile */ - fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.javalin.http.UploadedFile?, ctx: Context): ModelApiResponse + fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.javalin.http.UploadedFile? = null, ctx: Context): ModelApiResponse } diff --git a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt index 1783de4bca80..4a1cdb0a31e2 100644 --- a/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt +++ b/samples/server/petstore/kotlin-server/javalin/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt @@ -25,7 +25,7 @@ interface PetApiService { * @return Invalid pet value (status code 400) * @see PetApi#deletePet */ - fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): Unit + fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): Unit /** * GET /pet/findByStatus : Finds Pets by status @@ -87,7 +87,7 @@ interface PetApiService { * @return Invalid input (status code 405) * @see PetApi#updatePetWithForm */ - fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): Unit + fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): Unit /** * POST /pet/{petId}/uploadImage : uploads an image @@ -99,5 +99,5 @@ interface PetApiService { * @return successful operation (status code 200) * @see PetApi#uploadFile */ - fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.javalin.http.UploadedFile?): ModelApiResponse + fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.javalin.http.UploadedFile? = null): ModelApiResponse }