OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.77k stars 6.57k forks source link

[REQ] [Kotlin][Client] Option to remove Response<> wrapper in operation return types #15491

Open WonderCsabo opened 1 year ago

WonderCsabo commented 1 year ago

Currently if you use Coroutines (suspend modifier), your generated API functions will look like this:

@GET("users/{id}")
suspend fun user(@Path("id") id: Long): Response<User>

However there is another way to define the return type of the endpoint. You can omit the Response<> wrapper, and just return the User, like this:

@GET("users/{id}")
suspend fun user(@Path("id") id: Long): User

This is described here in the Retrofit documentation.

It would be great if OpenAPI generator offer this as well. It could be controlled by a CLI option, and it could generated the current Response<User> form by default.

WonderCsabo commented 1 year ago

@jimschubert @dr4ke616 @karismann @Zomzog @andrewemery @4brunu @yutaka0m

In case you accept this feature, i would gladly contribute this.

4brunu commented 1 year ago

Hi, thanks for taking the time to improve openapi-generator. We try to avoid creating CLI options, because when we have lots of them, it's hard to maintain all the possible configurations. I'm not sure, I think we could add this option to have direct access to the response, although that way we lose some information from the response.

WonderCsabo commented 1 year ago

I understand your point about the CLI option. I suggested one to retain the option for having more information about the response if users want that.

I also looked into the retrofit2/api.mustache template in mode detail, and the useRxJavaX options also omit this Response<> wrapper, only useCoroutines adds it.

4brunu commented 1 year ago

Are you suggesting to introduce a CLI option to enable/disable Response<> on all response types?

WonderCsabo commented 1 year ago

I understand the code which is generated is opinionated. But it should be consistent (currently Rx and coroutines are different on this matter).

For my use case, i would prefer without the wrapper of course. But i see there are other users of this tool.

4brunu commented 1 year ago

I think introducing a CLI option to enable/disable Response<> on all response types would fix the consistent. What do you think?

WonderCsabo commented 1 year ago

Yes, that would work for me. BTW, this CLI option would only apply to the retrofit2 library.

4brunu commented 1 year ago

It would be nice to apply to all configurations, but we can expand it in the future

WonderCsabo commented 1 year ago

To be honest i do not even know if this is applicable to the other libraries.

4brunu commented 1 year ago

Me neither because I'm not in front of a pc. Can you please open a PR with it?