Skip to content

Commit ef4f122

Browse files
committed
Merge branch 'fix/kotlin-optional-params-default-null' of https://github.com/halotukozak/openapi-generator into halotukozak-fix/kotlin-optional-params-default-null
2 parents d278939 + f9fb32e commit ef4f122

11 files changed

Lines changed: 37 additions & 20 deletions

File tree

‎modules/openapi-generator/src/main/resources/kotlin-client/libraries/jvm-ktor/api.mustache‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
5252
{{#returnType}}
5353
@Suppress("UNCHECKED_CAST")
5454
{{/returnType}}
55-
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}open suspend fun {{operationId}}({{#allParams}}{{{paramName}}}: {{{dataType}}}{{^required}}?{{/required}}{{^-last}}, {{/-last}}{{/allParams}}): HttpResponse<{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{#returnProperty}}{{#isNullable}}?{{/isNullable}}{{/returnProperty}}> {
55+
{{^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}}{{#returnProperty}}{{#isNullable}}?{{/isNullable}}{{/returnProperty}}> {
5656
5757
val localVariableAuthNames = listOf<String>({{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}})
5858

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface {{classname}}Service {
2727
{{/externalDocs}}
2828
* @see {{classname}}#{{operationId}}
2929
*/
30-
{{#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}}
30+
{{#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}}
3131
{{/operation}}
3232
}
3333
{{/operations}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface {{classname}}Service {
2929
{{/externalDocs}}
3030
* @see {{classname}}#{{operationId}}
3131
*/
32-
{{#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}}
32+
{{#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}}
3333
{{/operation}}
3434
}
3535
{{/operations}}

‎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
@@ -102,6 +102,23 @@ public void testUseResponseAsReturnType(Object useResponseAsReturnType, String e
102102
assertFileContainsLine(lines, "suspend fun deletePet(@Path(\"petId\") petId: kotlin.Long, @Header(\"api_key\") apiKey: kotlin.String? = null)" + expectedUnitResponse);
103103
}
104104

105+
@Test
106+
public void testOptionalParamsHaveDefaultNullJvmKtor() throws IOException {
107+
OpenAPI openAPI = readOpenAPI("3_0/kotlin/petstore.yaml");
108+
109+
KotlinClientCodegen codegen = createCodegen(ClientLibrary.JVM_KTOR);
110+
111+
ClientOptInput input = createClientOptInput(openAPI, codegen);
112+
113+
DefaultGenerator generator = new DefaultGenerator();
114+
enableOnlyApiGeneration(generator);
115+
116+
List<File> files = generator.opts(input).generate();
117+
File petApi = files.stream().filter(file -> file.getName().equals("PetApi.kt")).findAny().orElseThrow();
118+
119+
assertFileContains(petApi.toPath(), "apiKey: kotlin.String? = null");
120+
}
121+
105122
@Test
106123
public void testEnumDefaultForReferencedSchemaParameterJvmOkhttp4() throws IOException {
107124
OpenAPI openAPI = readOpenAPI("3_0/kotlin/enum-default-query.yaml");

‎samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/FakeApi.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import java.text.DateFormat
8888
* @param status2 number type (optional)
8989
* @return void
9090
*/
91-
open suspend fun updatePetWithFormNumber(petId: kotlin.Long, name: kotlin.String?, status: kotlin.Int?, status2: java.math.BigDecimal?): HttpResponse<Unit> {
91+
open suspend fun updatePetWithFormNumber(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.Int? = null, status2: java.math.BigDecimal? = null): HttpResponse<Unit> {
9292

9393
val localVariableAuthNames = listOf<String>("petstore_auth")
9494

‎samples/client/petstore/kotlin-jvm-ktor-gson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import java.text.DateFormat
8888
* @param apiKey (optional)
8989
* @return void
9090
*/
91-
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse<Unit> {
91+
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse<Unit> {
9292

9393
val localVariableAuthNames = listOf<String>("petstore_auth")
9494

@@ -261,7 +261,7 @@ import java.text.DateFormat
261261
* @param status Updated status of the pet (optional)
262262
* @return void
263263
*/
264-
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse<Unit> {
264+
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse<Unit> {
265265

266266
val localVariableAuthNames = listOf<String>("petstore_auth")
267267

@@ -300,7 +300,7 @@ import java.text.DateFormat
300300
* @return ModelApiResponse
301301
*/
302302
@Suppress("UNCHECKED_CAST")
303-
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>?): HttpResponse<ModelApiResponse> {
303+
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>? = null): HttpResponse<ModelApiResponse> {
304304

305305
val localVariableAuthNames = listOf<String>("petstore_auth")
306306

‎samples/client/petstore/kotlin-jvm-ktor-jackson/src/main/kotlin/org/openapitools/client/apis/PetApi.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
8585
* @param apiKey (optional)
8686
* @return void
8787
*/
88-
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse<Unit> {
88+
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse<Unit> {
8989

9090
val localVariableAuthNames = listOf<String>("petstore_auth")
9191

@@ -257,7 +257,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
257257
* @param status Updated status of the pet (optional)
258258
* @return void
259259
*/
260-
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse<Unit> {
260+
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse<Unit> {
261261

262262
val localVariableAuthNames = listOf<String>("petstore_auth")
263263

@@ -296,7 +296,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
296296
* @return ModelApiResponse
297297
*/
298298
@Suppress("UNCHECKED_CAST")
299-
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>?): HttpResponse<ModelApiResponse> {
299+
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>? = null): HttpResponse<ModelApiResponse> {
300300

301301
val localVariableAuthNames = listOf<String>("petstore_auth")
302302

‎samples/client/petstore/kotlin-jvm-ktor-kotlinx_serialization/src/main/kotlin/org/openapitools/client/apis/PetApi.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import io.ktor.http.ParametersBuilder
8383
* @param apiKey (optional)
8484
* @return void
8585
*/
86-
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?): HttpResponse<Unit> {
86+
open suspend fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null): HttpResponse<Unit> {
8787

8888
val localVariableAuthNames = listOf<String>("petstore_auth")
8989

@@ -256,7 +256,7 @@ import io.ktor.http.ParametersBuilder
256256
* @param status Updated status of the pet (optional)
257257
* @return void
258258
*/
259-
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?): HttpResponse<Unit> {
259+
open suspend fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null): HttpResponse<Unit> {
260260

261261
val localVariableAuthNames = listOf<String>("petstore_auth")
262262

@@ -295,7 +295,7 @@ import io.ktor.http.ParametersBuilder
295295
* @return ModelApiResponse
296296
*/
297297
@Suppress("UNCHECKED_CAST")
298-
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>?): HttpResponse<ModelApiResponse> {
298+
open suspend fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.ktor.client.request.forms.FormPart<io.ktor.client.request.forms.InputProvider>? = null): HttpResponse<ModelApiResponse> {
299299

300300
val localVariableAuthNames = listOf<String>("petstore_auth")
301301

‎samples/server/petstore/kotlin-server-required-and-nullable-properties/src/main/kotlin/org/openapitools/server/apis/DefaultApiService.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ interface DefaultApiService {
1313
* @return Successful operation (status code 200)
1414
* @see DefaultApi#addPet
1515
*/
16-
fun addPet(pet: Pet?, ctx: Context): Pet
16+
fun addPet(pet: Pet? = null, ctx: Context): Pet
1717
}

‎samples/server/petstore/kotlin-server/javalin-6/src/main/kotlin/org/openapitools/server/apis/PetApiService.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface PetApiService {
2828
* @return Invalid pet value (status code 400)
2929
* @see PetApi#deletePet
3030
*/
31-
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String?, ctx: Context): Unit
31+
fun deletePet(petId: kotlin.Long, apiKey: kotlin.String? = null, ctx: Context): Unit
3232

3333
/**
3434
* GET /pet/findByStatus : Finds Pets by status
@@ -95,7 +95,7 @@ interface PetApiService {
9595
* @return Invalid input (status code 405)
9696
* @see PetApi#updatePetWithForm
9797
*/
98-
fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String?, status: kotlin.String?, ctx: Context): Unit
98+
fun updatePetWithForm(petId: kotlin.Long, name: kotlin.String? = null, status: kotlin.String? = null, ctx: Context): Unit
9999

100100
/**
101101
* POST /pet/{petId}/uploadImage : uploads an image
@@ -108,5 +108,5 @@ interface PetApiService {
108108
* @return successful operation (status code 200)
109109
* @see PetApi#uploadFile
110110
*/
111-
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String?, file: io.javalin.http.UploadedFile?, ctx: Context): ModelApiResponse
111+
fun uploadFile(petId: kotlin.Long, additionalMetadata: kotlin.String? = null, file: io.javalin.http.UploadedFile? = null, ctx: Context): ModelApiResponse
112112
}

0 commit comments

Comments
 (0)