Skip to content

Commit

Permalink
Merge pull request #119 from outfoxx/feature/suspend-flow
Browse files Browse the repository at this point in the history
Allow choosing `suspend` modifier for `Flow` service methods (disabled by default)
  • Loading branch information
kdubb authored Dec 2, 2024
2 parents e77665b + e2f6810 commit 1ccc13d
Show file tree
Hide file tree
Showing 26 changed files with 623 additions and 267 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/setup-java@v4
with:
java-version: '21'
distribution: temurin
distribution: oracle

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ open class KotlinJAXRSGenerateCommand :
help = "Generate suspendable service methods for coroutine support",
).flag()

val coroutineFlowMethods by option(
"-flow-coroutines",
help = "Generate suspendable flow service methods for coroutine support",
).flag(default = false)

val reactiveResponseType by option(
"-reactive",
help = "Generic result type for reactive service methods",
Expand Down Expand Up @@ -76,6 +81,7 @@ open class KotlinJAXRSGenerateCommand :
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
coroutineFlowMethods,
coroutineServiceMethods,
reactiveResponseType,
explicitSecurityParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ class KotlinJAXRSCLITest {
assertThat(commandWithFalse.coroutineServiceMethods, equalTo(false))
}

@Test
fun `--flow-coroutines flag`() {

val commandWithTrue = KotlinJAXRSGenerateCommandTest()
assertDoesNotThrow { commandWithTrue.parse(arrayOf("-flow-coroutines", *requiredOptions)) }
assertThat(commandWithTrue.coroutineFlowMethods, equalTo(true))

val commandWithFalse = KotlinJAXRSGenerateCommandTest()
assertDoesNotThrow { commandWithFalse.parse(requiredOptions) }
assertThat(commandWithFalse.coroutineServiceMethods, equalTo(false))
}

@Test
fun `--reactive flag`() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ abstract class KotlinGenerator(
}
}
}
if (operation.successes.isEmpty()) {
processReturnType(
endPoint,
operation,
Response(),
null,
problemTypes,
typeBuilder,
functionBuilder,
UNIT,
)
}

operation.failures.forEach { response ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class KotlinJAXRSGenerator(
) {

class Options(
val coroutineFlowMethods: Boolean,
val coroutineServiceMethods: Boolean,
val reactiveResponseType: String?,
val explicitSecurityParameters: Boolean,
Expand Down Expand Up @@ -227,13 +228,13 @@ class KotlinJAXRSGenerator(

val mediaTypesForPayloads = response.payloads.mapNotNull { it.mediaType }

if (options.coroutineServiceMethods) {
functionBuilder.addModifiers(SUSPEND)
}

val isSSE = operation.findBoolAnnotation(SSE, generationMode) == true
val isFlow = operation.hasAnnotation(EventStream, generationMode) && options.coroutineServiceMethods

if ((isFlow && options.coroutineFlowMethods) || (!isFlow && options.coroutineServiceMethods)) {
functionBuilder.addModifiers(SUSPEND)
}

val reactive = operation.findBoolAnnotation(Reactive, generationMode) ?: reactiveDefault
if (reactive && reactiveResponseType != null && !isSSE && !isFlow) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ abstract class SwiftGenerator(
}
}
}
if (operation.successes.isEmpty()) {
processReturnType(
endPoint,
operation,
Response(),
null,
problemTypes,
typeBuilder,
functionBuilder,
VOID,
)
}

operation.failures.forEach { response ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,18 @@ abstract class TypeScriptGenerator(
}
}
}
if (operation.successes.isEmpty()) {
processReturnType(
endPoint,
operation,
Response(),
null,
problemTypes,
typeBuilder,
functionBuilder,
VOID,
)
}

operation.failures.forEach { response ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down Expand Up @@ -400,7 +401,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down Expand Up @@ -466,7 +468,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down Expand Up @@ -531,7 +534,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down Expand Up @@ -589,7 +593,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down Expand Up @@ -646,7 +651,8 @@ class RequestBodyParamTest {
shapeIndex,
typeRegistry,
KotlinJAXRSGenerator.Options(
false,
coroutineFlowMethods = false,
coroutineServiceMethods = false,
null,
false,
null,
Expand Down
Loading

0 comments on commit 1ccc13d

Please sign in to comment.