Skip to content

Commit 18b4846

Browse files
committed
~review fixes
1 parent 49c8a93 commit 18b4846

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal fun SerialDescriptor.extractParameters(index: Int): ProtoDesc {
8787
val annotation = annotations[i]
8888
if (annotation is ProtoNumber) {
8989
protoId = annotation.number
90-
checkFieldNumber(protoId, this)
90+
checkFieldNumber(protoId, i, this)
9191
} else if (annotation is ProtoType) {
9292
format = annotation.type
9393
} else if (annotation is ProtoPacked) {
@@ -121,16 +121,16 @@ internal fun extractProtoId(descriptor: SerialDescriptor, index: Int, zeroBasedD
121121
result = annotation.number
122122
// 0 or negative numbers are acceptable for enums
123123
if (!zeroBasedDefault) {
124-
checkFieldNumber(result, descriptor)
124+
checkFieldNumber(result, i, descriptor)
125125
}
126126
}
127127
}
128128
return result
129129
}
130130

131-
private fun checkFieldNumber(fieldNumber: Int, descriptor: SerialDescriptor) {
131+
private fun checkFieldNumber(fieldNumber: Int, propertyIndex: Int, descriptor: SerialDescriptor) {
132132
if (fieldNumber <= 0) {
133-
throw SerializationException("$fieldNumber is not allowed in ProtoNumber for ${descriptor.serialName}, because protobuf support field numbers in range 1..${Int.MAX_VALUE}}")
133+
throw SerializationException("$fieldNumber is not allowed in ProtoNumber for property '${descriptor.getElementName(propertyIndex)}' of '${descriptor.serialName}', because protobuf support field numbers in range 1..${Int.MAX_VALUE}")
134134
}
135135
}
136136

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ internal open class ProtobufDecoder(
4646
* array for the fast-path. Fast-path implies that elements are not marked with @ProtoId
4747
* explicitly or are monotonic and incremental (maybe, 1-indexed)
4848
*
49-
* Since the library allows the use of fields with proto ID 0,
50-
* it is necessary to initialize all elements, because there will always be one extra element
49+
* Initialize all elements, because there will always be one extra element as arrays are numbered from 0
50+
* but in protobuf field number starts from 1.
5151
* in the fast path array, which is missing from the descriptor
5252
*/
5353
val cache = IntArray(elements + 1) { -1 }

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ class AutoAssignIdsTest {
3030
assertEquals(w1.a, w2.a)
3131
assertEquals(w1.b, w2.b)
3232
}
33+
34+
class NestedClass {
35+
companion object
36+
}
37+
38+
object X
39+
40+
@Test
41+
fun testName() {
42+
println(X::class.qualifiedName)
43+
}
3344
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,29 @@ class InvalidFieldNumberTest {
4242

4343
@Test
4444
fun testSerializeZeroProtoNumber() {
45-
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber, because protobuf support field values in range 1..2147483647") {
45+
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf support field numbers in range 1..2147483647") {
4646
ProtoBuf.encodeToHexString(ZeroProtoNumber(42))
4747
}
4848
}
4949

5050
@Test
5151
fun testDeserializeZeroProtoNumber() {
52-
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber, because protobuf support field values in range 1..2147483647") {
52+
assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf support field numbers in range 1..2147483647") {
5353
ProtoBuf.decodeFromHexString<ZeroProtoNumber>("000f")
5454
}
5555
}
5656

5757
@Test
5858
fun testSerializeNegativeProtoNumber() {
59-
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber, because protobuf support field values in range 1..2147483647") {
59+
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf support field numbers in range 1..2147483647") {
6060
ProtoBuf.encodeToHexString(NegativeProtoNumber(42))
6161
}
6262
}
6363

6464
@Test
6565
fun testDeserializeNegativeProtoNumber() {
66-
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber, because protobuf support field values in range 1..2147483647") {
66+
assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf support field numbers in range 1..2147483647") {
6767
ProtoBuf.decodeFromHexString<NegativeProtoNumber>("000f")
6868
}
6969
}
70-
}
70+
}

0 commit comments

Comments
 (0)