Skip to content

Commit 624fd2f

Browse files
Fix bug with Unit return values in kotlin codegen (#465)
1 parent 70c30aa commit 624fd2f

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

sdk-api-kotlin-gen/src/main/resources/templates/ServiceDefinitionFactory.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class {{generatedClassSimpleName}}: dev.restate.sdk.endpoint.definition.ServiceD
2020
{{#if isExclusive}}dev.restate.sdk.endpoint.definition.HandlerType.EXCLUSIVE{{else if isWorkflow}}dev.restate.sdk.endpoint.definition.HandlerType.WORKFLOW{{else}}dev.restate.sdk.endpoint.definition.HandlerType.SHARED{{/if}},
2121
{{inputSerdeRef}},
2222
{{outputSerdeRef}},
23-
dev.restate.sdk.kotlin.HandlerRunner.of({{serdeFactoryRef}}, handlerRunnerOptions, bindableService::{{name}})
23+
dev.restate.sdk.kotlin.HandlerRunner.{{#if outputEmpty}}ofEmptyReturn{{else}}of{{/if}}({{serdeFactoryRef}}, handlerRunnerOptions, bindableService::{{name}})
2424
){{#if inputAcceptContentType}}.withAcceptContentType("{{inputAcceptContentType}}"){{/if}}{{#unless @last}},{{/unless}}
2525
{{/handlers}}
2626
)

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/HandlerRunner.kt

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ internal constructor(
3333
companion object {
3434
private val LOG = LogManager.getLogger(HandlerRunner::class.java)
3535

36-
/** Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. */
36+
/**
37+
* Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. Please note this
38+
* may be subject to breaking changes.
39+
*/
3740
fun <REQ, RES, CTX : Context> of(
3841
contextSerdeFactory: SerdeFactory,
3942
options: Options = Options.DEFAULT,
@@ -42,14 +45,53 @@ internal constructor(
4245
return HandlerRunner(runner, contextSerdeFactory, options)
4346
}
4447

45-
/** Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. */
48+
/**
49+
* Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. Please note this
50+
* may be subject to breaking changes.
51+
*/
4652
fun <RES, CTX : Context> of(
4753
contextSerdeFactory: SerdeFactory,
4854
options: Options = Options.DEFAULT,
4955
runner: suspend (CTX) -> RES,
5056
): HandlerRunner<Unit, RES, CTX> {
5157
return HandlerRunner({ ctx: CTX, _: Unit -> runner(ctx) }, contextSerdeFactory, options)
5258
}
59+
60+
/**
61+
* Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. Please note this
62+
* may be subject to breaking changes.
63+
*/
64+
fun <REQ, CTX : Context> ofEmptyReturn(
65+
contextSerdeFactory: SerdeFactory,
66+
options: Options = Options.DEFAULT,
67+
runner: suspend (CTX, REQ) -> Unit,
68+
): HandlerRunner<REQ, Unit, CTX> {
69+
return HandlerRunner(
70+
{ ctx: CTX, req: REQ ->
71+
runner(ctx, req)
72+
Unit
73+
},
74+
contextSerdeFactory,
75+
options)
76+
}
77+
78+
/**
79+
* Factory method for [dev.restate.sdk.kotlin.HandlerRunner], used by codegen. Please note this
80+
* may be subject to breaking changes.
81+
*/
82+
fun <CTX : Context> ofEmptyReturn(
83+
contextSerdeFactory: SerdeFactory,
84+
options: Options = Options.DEFAULT,
85+
runner: suspend (CTX) -> Unit,
86+
): HandlerRunner<Unit, Unit, CTX> {
87+
return HandlerRunner(
88+
{ ctx: CTX, _: Unit ->
89+
runner(ctx)
90+
Unit
91+
},
92+
contextSerdeFactory,
93+
options)
94+
}
5395
}
5496

5597
override fun run(

sdk-core/src/test/kotlin/dev/restate/sdk/core/kotlinapi/CodegenTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
99
package dev.restate.sdk.core.kotlinapi
1010

11+
import dev.restate.common.Slice
1112
import dev.restate.common.Target
1213
import dev.restate.sdk.annotation.*
1314
import dev.restate.sdk.core.TestDefinitions
@@ -125,6 +126,13 @@ class CodegenTest : TestDefinitions.TestSuite {
125126
.returnNull(request) {}
126127
.await()
127128
}
129+
130+
@Exclusive
131+
suspend fun badReturnTypeInferred(context: ObjectContext): Unit {
132+
CodegenTestCornerCasesClient.fromContext(context, context.key())
133+
.send()
134+
.badReturnTypeInferred()
135+
}
128136
}
129137

130138
@Workflow
@@ -337,6 +345,19 @@ class CodegenTest : TestDefinitions.TestSuite {
337345
null),
338346
outputCmd(jsonSerde<String?>(), null),
339347
END_MESSAGE),
348+
testInvocation({ CornerCases() }, "badReturnTypeInferred")
349+
.withInput(startMessage(1, "mykey"), inputCmd())
350+
.onlyBidiStream()
351+
.expectingOutput(
352+
oneWayCallCmd(
353+
1,
354+
Target.virtualObject(
355+
"CodegenTestCornerCases", "mykey", "badReturnTypeInferred"),
356+
null,
357+
null,
358+
Slice.EMPTY),
359+
outputCmd(),
360+
END_MESSAGE),
340361
)
341362
}
342363
}

0 commit comments

Comments
 (0)