-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlin Coroutines Support
- Loading branch information
Showing
13 changed files
with
283 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
plugin/src/main/java/com/yelp/codegen/KotlinCoroutineGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.yelp.codegen | ||
|
||
import io.swagger.codegen.CodegenOperation | ||
import io.swagger.models.Model | ||
import io.swagger.models.Operation | ||
import io.swagger.models.Swagger | ||
|
||
open class KotlinCoroutineGenerator : KotlinGenerator() { | ||
|
||
init { | ||
templateDir = "kotlin" | ||
} | ||
|
||
override fun getName() = "kotlin-coroutines" | ||
|
||
override fun wrapResponseType(imports: MutableSet<String>, responsePrimitiveType: String) = responsePrimitiveType | ||
|
||
override fun getNoResponseType(imports: MutableSet<String>) = "Unit" | ||
|
||
/** | ||
* Overriding the behavior of [KotlinGenerator] to make sure operations are rendered as `suspend fun` rather | ||
* than just plain `fun`. | ||
*/ | ||
override fun fromOperation( | ||
path: String?, | ||
httpMethod: String?, | ||
operation: Operation?, | ||
definitions: MutableMap<String, Model>?, | ||
swagger: Swagger? | ||
): CodegenOperation { | ||
val codegenOperation = super.fromOperation(path, httpMethod, operation, definitions, swagger) | ||
codegenOperation.vendorExtensions[X_FUNCTION_QUALIFIERS] = "suspend" | ||
return codegenOperation | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
plugin/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
com.yelp.codegen.KotlinGenerator | ||
com.yelp.codegen.KotlinCoroutineGenerator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
buildscript { | ||
repositories { | ||
mavenLocal() | ||
gradlePluginPortal() | ||
google() | ||
mavenCentral() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath "com.android.tools.build:gradle:3.5.0" | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50" | ||
classpath "com.yelp.codegen:plugin:1.2.0" | ||
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.1" | ||
} | ||
} | ||
|
||
apply plugin: "com.android.library" | ||
apply plugin: "kotlin-android" | ||
apply plugin: "com.yelp.codegen.plugin" | ||
apply plugin: "io.gitlab.arturbosch.detekt" | ||
|
||
android { | ||
compileSdkVersion = 28 | ||
defaultConfig { | ||
minSdkVersion 21 | ||
targetSdkVersion 28 | ||
versionCode = 1 | ||
versionName = "1.0" | ||
} | ||
} | ||
|
||
dependencies { | ||
// Kotlin | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.41" | ||
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.41" | ||
|
||
// Moshi + OkHttp + Retrofit | ||
implementation "com.squareup.moshi:moshi:1.8.0" | ||
implementation "com.squareup.moshi:moshi-adapters:1.8.0" | ||
implementation "com.squareup.moshi:moshi-kotlin:1.8.0" | ||
implementation "com.squareup.okhttp3:okhttp:3.12.3" | ||
implementation "com.squareup.retrofit2:retrofit:2.6.1" | ||
implementation "com.squareup.retrofit2:converter-moshi:2.6.1" | ||
|
||
// Date Support | ||
implementation "com.jakewharton.threetenabp:threetenabp:1.2.1" | ||
|
||
// Testing Dependencies | ||
testImplementation "junit:junit:4.12" | ||
testImplementation "com.squareup.okhttp3:mockwebserver:3.12.3" | ||
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.3" | ||
} | ||
|
||
generateSwagger { | ||
platform = "kotlin-coroutines" | ||
packageName = "com.yelp.codegen.samples.kotlincoroutines" | ||
specName = "kotlincoroutines" | ||
inputFile = file("../sample_specs.json") | ||
outputDir = file("./src/main/java/") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
detekt { | ||
toolVersion = "1.0.0-RC16" | ||
input = files("src/test") | ||
filters = ".*/resources/.*,.*/build/.*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.yelp.samplelibrary"/> |
72 changes: 72 additions & 0 deletions
72
...utines/src/test/java/com/yelp/codegen/samples/kotlincoroutines/CoroutinesEndpointsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.yelp.codegen.samples.kotlincoroutines | ||
|
||
import com.yelp.codegen.samples.kotlincoroutines.apis.ResourceApi | ||
import com.yelp.codegen.samples.kotlincoroutines.models.PropertyModel | ||
import com.yelp.codegen.samples.kotlincoroutines.tools.CoroutineDispatcherRule | ||
import com.yelp.codegen.samples.kotlincoroutines.tools.MockServerApiRule | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import okhttp3.mockwebserver.MockResponse | ||
import org.junit.Assert.* | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@ExperimentalCoroutinesApi | ||
class CoroutinesEndpointsTest { | ||
|
||
@get:Rule | ||
val mockServerRule = MockServerApiRule() | ||
|
||
@get:Rule | ||
val coroutinesRule = CoroutineDispatcherRule() | ||
|
||
@Test | ||
fun emptyEndpointTest() { | ||
mockServerRule.server.enqueue(MockResponse().setBody("{}")) | ||
|
||
coroutinesRule.runBlockingTest { | ||
val returned = mockServerRule.getApi<ResourceApi>().getEmptyEndpoint() | ||
assertNotNull(returned) | ||
} | ||
} | ||
|
||
@Test | ||
fun propertyEndpointTest_withStringProperty() { | ||
mockServerRule.server.enqueue(MockResponse().setBody(""" | ||
{ | ||
"string_property": "string" | ||
} | ||
""".trimIndent())) | ||
|
||
coroutinesRule.runBlockingTest { | ||
val returned = mockServerRule.getApi<ResourceApi>().getPropertyEndpoint("string") | ||
assertEquals("string", returned.stringProperty) | ||
assertNull(returned.enumProperty) | ||
} | ||
} | ||
|
||
@Test | ||
fun propertyEndpointTest_withEnumProperty() { | ||
mockServerRule.server.enqueue(MockResponse().setBody(""" | ||
{ | ||
"enum_property": "VALUE1" | ||
} | ||
""".trimIndent())) | ||
|
||
coroutinesRule.runBlockingTest { | ||
val returned = mockServerRule.getApi<ResourceApi>().getPropertyEndpoint("string") | ||
assertEquals(PropertyModel.EnumPropertyEnum.VALUE1, returned.enumProperty) | ||
assertNull(returned.stringProperty) | ||
} | ||
} | ||
|
||
@Test | ||
fun propertyEndpointTest_withEmptyObject() { | ||
mockServerRule.server.enqueue(MockResponse().setBody("{}")) | ||
|
||
coroutinesRule.runBlockingTest { | ||
val returned = mockServerRule.getApi<ResourceApi>().getPropertyEndpoint("string") | ||
assertNull(returned.stringProperty) | ||
assertNull(returned.enumProperty) | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../src/test/java/com/yelp/codegen/samples/kotlincoroutines/tools/CoroutineDispatcherRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.yelp.codegen.samples.kotlincoroutines.tools | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
import kotlinx.coroutines.test.TestCoroutineScope | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.rules.ExternalResource | ||
|
||
/** | ||
* JUnit rule to create a Retrofit instance able to interact with the [MockWebServer] and to create Retrofit instances | ||
* on the fly as needed. | ||
*/ | ||
@ExperimentalCoroutinesApi | ||
class CoroutineDispatcherRule : ExternalResource() { | ||
|
||
private val testCoroutineDispatcher = TestCoroutineDispatcher() | ||
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher) | ||
|
||
override fun before() { | ||
super.before() | ||
Dispatchers.setMain(testCoroutineDispatcher) | ||
} | ||
|
||
override fun after() { | ||
Dispatchers.resetMain() | ||
testCoroutineScope.cleanupTestCoroutines() | ||
super.after() | ||
} | ||
|
||
fun runBlockingTest(block: suspend CoroutineScope.() -> Unit) = | ||
runBlocking(Dispatchers.Main, block) | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...utines/src/test/java/com/yelp/codegen/samples/kotlincoroutines/tools/MockServerApiRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.yelp.codegen.samples.kotlincoroutines.tools | ||
|
||
import okhttp3.mockwebserver.MockWebServer | ||
import org.junit.rules.ExternalResource | ||
import retrofit2.Retrofit | ||
|
||
/** | ||
* JUnit rule to create a Retrofit instance able to interact with the [MockWebServer] and to create Retrofit instances | ||
* on the fly as needed. | ||
*/ | ||
class MockServerApiRule : ExternalResource() { | ||
|
||
lateinit var retrofit: Retrofit | ||
val server = MockWebServer() | ||
|
||
override fun before() { | ||
super.before() | ||
server.start() | ||
|
||
retrofit = Retrofit.Builder() | ||
.addConverterFactory(GeneratedCodeConverters.converterFactory()) | ||
.baseUrl(server.url("/")) | ||
.build() | ||
} | ||
|
||
override fun after() { | ||
server.shutdown() | ||
super.after() | ||
} | ||
|
||
inline fun <reified T> getApi(): T { | ||
return retrofit.create(T::class.java) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters