Skip to content

Commit 24c0751

Browse files
committed
pb: Fix bytes copy in oneOf
1 parent 2ff4359 commit 24c0751

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

protobuf/protobuf-core/src/commonTest/kotlin/kotlinx/rpc/protobuf/test/CopyTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
package kotlinx.rpc.protobuf.test
66

7+
import Equals
8+
import OneOfMsg
79
import invoke
810
import test.submsg.Other
911
import test.submsg.invoke
1012
import kotlin.collections.iterator
1113
import kotlin.test.Test
14+
import kotlin.test.assertContentEquals
1215
import kotlin.test.assertEquals
1316
import kotlin.test.assertNull
1417
import kotlin.test.assertTrue
@@ -216,6 +219,19 @@ class CopyTest {
216219
assertEquals(listOf<Byte>(1, 2, 3), copy.bytes?.toList())
217220
}
218221

222+
@Test
223+
fun `copy with bytes in oneof - mutating must not affect copy`() {
224+
val userBytes = byteArrayOf(1, 2, 3)
225+
val original = OneOfMsg {
226+
field = OneOfMsg.Field.Bytes(userBytes)
227+
}
228+
val copy = original.copy()
229+
userBytes[0] = 99
230+
231+
assertContentEquals(byteArrayOf(99, 2, 3), (original.field as OneOfMsg.Field.Bytes).value)
232+
assertContentEquals(byteArrayOf(1, 2, 3), (copy.field as OneOfMsg.Field.Bytes).value)
233+
}
234+
219235
@Test
220236
fun `copy with empty collections`() {
221237
val msg = Repeated {

protobuf/protobuf-core/src/commonTest/proto/oneof.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ message OneOfMsg {
88
fixed64 fixed = 3;
99
test.submsg.Other other = 4;
1010
kotlinx.rpc.protobuf.test.MyEnum enum = 5;
11+
bytes bytes = 6;
1112
}
1213
}
1314

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ class ModelToProtobufKotlinCommonGenerator(
441441
contextReceiver = oneOfFullName,
442442
) {
443443
// check if the type is copy by value (no need for deep copy)
444-
val copyByValue = { type: FieldType -> type is FieldType.IntegralType || type is FieldType.Enum }
444+
val copyByValue = { type: FieldType ->
445+
(type is FieldType.IntegralType && type != FieldType.IntegralType.BYTES) || type is FieldType.Enum
446+
}
445447

446448
// if all variants are integral or enum types, we can just return this directly.
447449
val fastPath = oneOf.variants.all { copyByValue(it.type) }

0 commit comments

Comments
 (0)