You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
There's an issue generating models for API spec that contains enums in request body for Kotlin and Retrofit 2. Enums are generated, but the request method then uses Strings as parameters. If a default value is provided, the result is even code that doesn't compile at all, because the default value doesn't have quotation marks for String.
Observe the generated Kotlin code of the file DefaultApi.kt
Here's the generated file:
packagecom.example.openapienumbug.openapi.apiimportorg.openapitools.client.infrastructure.CollectionFormats.*importretrofit2.http.*importretrofit2.Responseimportokhttp3.RequestBodyimportcom.squareup.moshi.Jsonimportcom.example.openapienumbug.openapi.entities.ResponseEntityinterfaceDefaultApi {
/** * enum for parameter enumPropType*/enumclassEnumPropTypeTestPost(valvalue: kotlin.String) {
@Json(name ="test1") TEST1("test1"),
@Json(name ="test2") TEST2("test2")
}
/** * Test * Test * Responses: * - 200: Test * * @param enumPropType Test (default to test1) * @return [ResponseEntity]*/
@FormUrlEncoded
@POST("test")
suspendfuntestPost(@Field("enum_prop_type") enumPropType: kotlin.String = test1): Response<ResponseEntity>
}
Notice how the EnumPropTypeTestPost is not used inside the testPost method, for the enumPropType. A String is used as the type. On top of that, the default value test1 is not wrapped by quotation marks for a String parameter.
This would be the expected result:
packagecom.example.openapienumbug.openapi.apiimportorg.openapitools.client.infrastructure.CollectionFormats.*importretrofit2.http.*importretrofit2.Responseimportokhttp3.RequestBodyimportcom.squareup.moshi.Jsonimportcom.example.openapienumbug.openapi.entities.ResponseEntityinterfaceDefaultApi {
/** * enum for parameter enumPropType*/enumclassEnumPropTypeTestPost(valvalue: kotlin.String) {
@Json(name ="test1") TEST1("test1"),
@Json(name ="test2") TEST2("test2")
}
/** * Test * Test * Responses: * - 200: Test * * @param enumPropType Test (default to test1) * @return [ResponseEntity]*/
@FormUrlEncoded
@POST("test")
suspendfuntestPost(@Field("enum_prop_type") enumPropType:EnumPropTypeTestPost = EnumPropTypeTestPost.TEST1): Response<ResponseEntity>
}
Related issues/PRs
I did not find anything related
Suggest a fix
No suggestion on my side
The text was updated successfully, but these errors were encountered:
The fact that Strings and not Enums are used in the parameters may not necessarily be a bug. Retrofit does not support enums for @FormUrlEncoded properly, one would need to provide also some converter for them. Maybe it was intentional for @FormUrlEncoded to use strings.
But the fact that when default value is provided the resulting code does not compile is a clear bug.
Bug Report Checklist
Description
There's an issue generating models for API spec that contains enums in request body for Kotlin and Retrofit 2. Enums are generated, but the request method then uses Strings as parameters. If a default value is provided, the result is even code that doesn't compile at all, because the default value doesn't have quotation marks for String.
openapi-generator version
7.9.0
OpenAPI declaration file content or url
Generation Details
Setup of the gradle task that fails to generate proper Kotlin code:
Steps to reproduce
Run the
openApiGenerateTestApi
task in the demo app provided here:OpenApiEnumBug.zip
Observe the generated Kotlin code of the file
DefaultApi.kt
Here's the generated file:
Notice how the
EnumPropTypeTestPost
is not used inside thetestPost
method, for theenumPropType
. A String is used as the type. On top of that, the default valuetest1
is not wrapped by quotation marks for a String parameter.This would be the expected result:
Related issues/PRs
I did not find anything related
Suggest a fix
No suggestion on my side
The text was updated successfully, but these errors were encountered: