Skip to content

Commit 2ec2edc

Browse files
No checking proto id duplication
1 parent c225811 commit 2ec2edc

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/ProtobufDecoding.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ internal open class ProtobufDecoder(
5959
}
6060
}
6161
indexCache = cache
62-
63-
// Class type should have unique proto id for its elements
64-
require(descriptor.kind != StructureKind.CLASS ||
65-
indexCache?.toSet()?.size == elements) {
66-
"Duplicated proto number in ${descriptor.serialName}."
67-
}
6862
} else {
6963
populateCacheMap(descriptor, elements)
7064
}
@@ -92,10 +86,7 @@ internal open class ProtobufDecoder(
9286
}
9387

9488
private fun MutableMap<Int, Int>.putProtoId(protoId: Int, index: Int) {
95-
val old = put(protoId, index)
96-
require(old == null) {
97-
"Duplicated proto number $protoId in ${descriptor.serialName} for elements: ${descriptor.getElementName(index)}, ${descriptor.getElementName(old!!)}."
98-
}
89+
put(protoId, index)
9990
}
10091

10192
private fun getIndexByNum(protoNum: Int): Int {

formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufOneOfTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class ProtobufOneOfTest {
546546
@Test
547547
fun testDuplicatedIdClass() {
548548
val duplicated = DuplicatingIdData(DuplicatingIdStringType("foo"), 42)
549+
// Fine to encode duplicated proto number properties in wire data
549550
ProtoBuf.encodeToHexString(duplicated).also {
550551
/**
551552
* 3:LEN {"foo"}
@@ -554,8 +555,11 @@ class ProtobufOneOfTest {
554555
assertEquals("1a03666f6f182a", it)
555556
}
556557

558+
// Without checking duplication of proto numbers,
559+
// ProtoBuf just throw exception about wrong wire type
557560
assertFailsWithMessage<IllegalArgumentException>(
558-
"Duplicated proto number 3 in kotlinx.serialization.protobuf.ProtobufOneOfTest.DuplicatingIdData for elements: d, bad."
561+
// "Duplicated proto number 3 in kotlinx.serialization.protobuf.ProtobufOneOfTest.DuplicatingIdData for elements: d, bad."
562+
"Expected wire type 0, but found 2"
559563
) {
560564
/**
561565
* 3:LEN {"foo"}

0 commit comments

Comments
 (0)