Skip to content

Commit

Permalink
No checking proto id duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhikang0916 committed Apr 25, 2024
1 parent c225811 commit 2ec2edc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ internal open class ProtobufDecoder(
}
}
indexCache = cache

// Class type should have unique proto id for its elements
require(descriptor.kind != StructureKind.CLASS ||
indexCache?.toSet()?.size == elements) {
"Duplicated proto number in ${descriptor.serialName}."
}
} else {
populateCacheMap(descriptor, elements)
}
Expand Down Expand Up @@ -92,10 +86,7 @@ internal open class ProtobufDecoder(
}

private fun MutableMap<Int, Int>.putProtoId(protoId: Int, index: Int) {
val old = put(protoId, index)
require(old == null) {
"Duplicated proto number $protoId in ${descriptor.serialName} for elements: ${descriptor.getElementName(index)}, ${descriptor.getElementName(old!!)}."
}
put(protoId, index)
}

private fun getIndexByNum(protoNum: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ class ProtobufOneOfTest {
@Test
fun testDuplicatedIdClass() {
val duplicated = DuplicatingIdData(DuplicatingIdStringType("foo"), 42)
// Fine to encode duplicated proto number properties in wire data
ProtoBuf.encodeToHexString(duplicated).also {
/**
* 3:LEN {"foo"}
Expand All @@ -554,8 +555,11 @@ class ProtobufOneOfTest {
assertEquals("1a03666f6f182a", it)
}

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

0 comments on commit 2ec2edc

Please sign in to comment.