icerockdev / moko-network

Network components with codegeneration of rest api for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev
Apache License 2.0
153 stars 29 forks source link

Make APIs optionally open to allow create API fakes #36

Closed xsveda closed 3 years ago

xsveda commented 4 years ago

We were used to generate Retrofit interfaces from swagger and to make development easier (server is not always available) we created fake implementation of those Retrofit interfaces that serve static hardcoded responses without touching the server.

Now when we generate Api classes based on Ktor it is not possible to sub-class them for stubs anymore.

It would be great to have an optional switch to make both Api classes and all its functions open in api.mustache.

Current class definition: {{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}}

Suggested class definition: {{#openApi}}open {{/openApi}}{{#nonPublicApi}}internal {{/nonPublicApi}}class {{classname}}

Alex009 commented 3 years ago

@xsveda maybe we should generate interface in addition to implementation? to not override generated implementation, but implement fake implementation. what do you think about it?

example:

interface ProfileApi {
    suspend fun getProfile(profileId: Int): Profile
}

class ProfileApiImpl(...): ProfileApi {
    override suspend fun getProfile(profileId: Int): Profile
}
xsveda commented 3 years ago

Sure, until they will be generics-free (for better Swift interoperability) I don't see an issue. On the other hand, there is a completely re-write of KMP client in openapi-generator itself (https://github.com/OpenAPITools/openapi-generator/pull/7353) coming hopefully soon, that will also solve this.

Alex009 commented 3 years ago

thanks for link! i will follow the updates of this PR. later already openapi-generator got multiplatform support, but with kotlin updates it was not updated so we continue using moko-network..maybe in this time multiplatform support will be better

Alex009 commented 3 years ago

your suggestion with open class implemented in #52

Alex009 commented 3 years ago

interfaces idea moved to #53