Skip to content

Commit d5b466f

Browse files
committed
grpc-pb: Add oneOf message merge test
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 1454a8f commit d5b466f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/pb/ProtosTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import OneOfMsg
88
import OneOfMsgInternal
99
import Outer
1010
import OuterInternal
11+
import asInternal
12+
import encodeWith
1113
import invoke
1214
import kotlinx.io.Buffer
1315
import kotlinx.rpc.grpc.codec.MessageCodec
@@ -177,6 +179,30 @@ class ProtosTest {
177179
}
178180
}
179181

182+
@Test
183+
fun testOneOfMsgMerging() {
184+
val part1 = OneOfMsg {
185+
field = OneOfMsg.Field.Other(Other { arg2 = "arg2" })
186+
}
187+
val part2 = OneOfMsg {
188+
field = OneOfMsg.Field.Other(Other { arg1 = "arg1" })
189+
}
190+
191+
val buffer = Buffer()
192+
val encoder = WireEncoder(buffer)
193+
part1.asInternal().encodeWith(encoder)
194+
part2.asInternal().encodeWith(encoder)
195+
encoder.flush()
196+
197+
198+
val decoded = OneOfMsgInternal.CODEC.decode(buffer)
199+
assertIs<OneOfMsg.Field.Other>(decoded.field)
200+
val decodedOther = (decoded.field as OneOfMsg.Field.Other).value
201+
assertEquals("arg2", decodedOther.arg2)
202+
assertEquals("arg1", decodedOther.arg1)
203+
assertEquals(null, decodedOther.arg3)
204+
}
205+
180206
@Test
181207
fun testOneOfLastWins() {
182208
// write two values on the oneOf field.

protoc-gen/src/main/kotlin/kotlinx/rpc/protobuf/ModelToKotlinCommonGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class ModelToKotlinCommonGenerator(
265265
code("val tag = decoder.readTag() ?: break // EOF, we read the whole message")
266266
whenBlock {
267267
declaration.fields().forEach { (_, field) -> readMatchCase(field) }
268-
whenCase("else") { code("TODO(\"Handle unknown fields\")") }
268+
whenCase("else") { code("TODO(\"Handle unknown fields: \$tag\")") }
269269
}
270270
}
271271
ifBranch(
@@ -394,7 +394,7 @@ class ModelToKotlinCommonGenerator(
394394

395395
is FieldType.Message -> {
396396
val internalClassName = fieldType.dec.value.internalClassFullName()
397-
code("decoder.readMessage($lvalue.asInternal(), $internalClassName::decodeWith)")
397+
code("decoder.readMessage($lvalue.asInternal(), $internalClassName.CODEC::decodeWith)")
398398
}
399399

400400
is FieldType.Map -> TODO()

0 commit comments

Comments
 (0)