[BUG][Kotlin][Retrofit2] Generator should support optional form parameter
Created by: shanselm-ergon
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
As stated in #8809 (closed) there is a similar problem with the form elements, which can be marked optional, but the code generator does not make them optional.
Experienced:
@Multipart
@POST("do/something")
fun saveWithAttachment(@Part("subject") subject: kotlin.String, @Part file1: MultipartBody.Part): Single<SomethingDTO>
Expected:
@Multipart
@POST("do/something")
fun saveWithAttachment(@Part("subject") subject: kotlin.String? = null, @Part file1: MultipartBody.Part? = null): Single<SomethingDTO>
openapi-generator version
5.1.0
OpenAPI declaration file content or url
"/do/something": {
"post": {
"tags": [
"doSomething"
],
"operationId": "saveWithAttachment",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "subject",
"in": "formData",
"required": false,
"type": "string"
},
{
"name": "file1",
"in": "formData",
"required": false,
"type": "file"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"$ref": "#/definitions/SomethingDTO"
}
}
}
}
}
Related issues/PRs
See #8809 (closed)
Suggest a fix
In formParams.mustache
change
{{#isFormParam}}{{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{{paramName}}}: {{{dataType}}}{{/isFile}}{{#isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} {{{paramName}}}: MultipartBody.Part{{/isFile}}{{/isFormParam}}
to:
{{#isFormParam}}{{^isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field{{/isMultipart}}("{{baseName}}") {{{paramName}}}: {{{dataType}}}{{#required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/required}}{{^required}}?{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{^defaultValue}} = null{{/defaultValue}}{{/required}}{{/isFile}}{{#isFile}}{{#isMultipart}}@Part{{/isMultipart}}{{^isMultipart}}@Field("{{baseName}}"){{/isMultipart}} {{{paramName}}}: MultipartBody.Part{{^required}}? = null{{/required}}{{/isFile}}{{/isFormParam}}