Skip to content

Commit c5f0503

Browse files
Refine exception msg if bad oneof def found
1 parent f99c016 commit c5f0503

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,11 @@ internal fun SerialDescriptor.getAllOneOfSerializerOfField(
117117
PolymorphicKind.OPEN -> serializersModule.getPolymorphicDescriptors(this)
118118
PolymorphicKind.SEALED -> getElementDescriptor(1).elementDescriptors.toList()
119119
else -> throw IllegalArgumentException("Class ${this.serialName} should be abstract or sealed or interface to be used as @ProtoOneOf property.")
120-
}.filter { desc ->
121-
desc.getElementAnnotations(0).any { anno -> anno is ProtoNumber }
120+
}.onEach { desc ->
121+
if (desc.getElementAnnotations(0).none { anno -> anno is ProtoNumber }) {
122+
throw IllegalArgumentException("${desc.serialName} implementing oneOf type ${this.serialName} should have @ProtoNumber annotation in its single property.")
122123
}
124+
}
123125
}
124126

125127
internal fun SerialDescriptor.getActualOneOfSerializer(

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ProtobufOneOfTest {
4949
@Test
5050
fun testOneOfIntType() {
5151
val dataInt = OneOfData(IntType(42), "foo")
52-
val intString = ProtoBuf.encodeToHexString(OneOfData.serializer(), dataInt).also { println(it) }
52+
val intString = ProtoBuf.encodeToHexString(OneOfData.serializer(), dataInt)
5353
/**
5454
* 5:VARINT 42
5555
* 3:LEN {"foo"}
@@ -60,7 +60,7 @@ class ProtobufOneOfTest {
6060
@Test
6161
fun testOneOfStringType() {
6262
val dataString = OneOfData(StringType("bar"), "foo")
63-
val stringString = ProtoBuf.encodeToHexString(OneOfData.serializer(), dataString).also { println(it) }
63+
val stringString = ProtoBuf.encodeToHexString(OneOfData.serializer(), dataString)
6464
/**
6565
* 6:LEN {"bar"}
6666
* 3:LEN {"foo"}
@@ -92,7 +92,6 @@ class ProtobufOneOfTest {
9292
fun testOneOfIntTypeNullable() {
9393
val dataInt = OneOfDataNullable(IntType(42), "foo")
9494
ProtoBuf.encodeToHexString(OneOfDataNullable.serializer(), dataInt).also {
95-
println(it)
9695
/**
9796
* 5:VARINT 42
9897
* 3:LEN {"foo"}
@@ -106,7 +105,6 @@ class ProtobufOneOfTest {
106105
fun testOneOfStringTypeNullable() {
107106
val dataString = OneOfDataNullable(StringType("bar"), "foo")
108107
ProtoBuf.encodeToHexString(OneOfDataNullable.serializer(), dataString).also {
109-
println(it)
110108
/**
111109
* 6:LEN {"bar"}
112110
* 3:LEN {"foo"}
@@ -115,7 +113,6 @@ class ProtobufOneOfTest {
115113
}
116114
val dataStringNull = OneOfDataNullable(null, "foo")
117115
ProtoBuf.encodeToHexString(OneOfDataNullable.serializer(), dataStringNull).also {
118-
println(it)
119116
/**
120117
* 3:LEN {"foo"}
121118
*/
@@ -199,7 +196,6 @@ class ProtobufOneOfTest {
199196
fun testEncodeNestedStruct() {
200197
val data = OneOfData(NestedIntType(IntType(32)), "foo")
201198
ProtoBuf.encodeToHexString(OneOfData.serializer(), data).also {
202-
println(it)
203199
/**
204200
* 7:LEN {5:VARINT 32}
205201
* 3:LEN {"foo"}
@@ -216,7 +212,6 @@ class ProtobufOneOfTest {
216212
* 3:LEN {"foo"}
217213
*/
218214
ProtoBuf.decodeFromHexString<OneOfData>("3a0228201a03666f6f").also {
219-
println(it)
220215
assertEquals(data, it)
221216
}
222217
}
@@ -255,7 +250,6 @@ class ProtobufOneOfTest {
255250
fun testEncodeNestedOneOf() {
256251
val data = OneOfData(NestedOneOfType(i = InnerNested(InnerInt(32))), "foo")
257252
ProtoBuf.encodeToHexString(OneOfData.serializer(), data).also {
258-
println(it)
259253
/**
260254
* 9:LEN {1:VARINT 32}
261255
* 3:LEN {"foo"}
@@ -272,7 +266,6 @@ class ProtobufOneOfTest {
272266
* 3:LEN {"foo"}
273267
*/
274268
ProtoBuf.decodeFromHexString<OneOfData>("4a0208201a03666f6f").also {
275-
println(it)
276269
/**
277270
* 9: {1: 32}
278271
* 3: {"foo"}
@@ -312,7 +305,6 @@ class ProtobufOneOfTest {
312305
OtherStringType("bar")
313306
)
314307
buf.encodeToHexString(DoubleOneOfElement.serializer(), data).also {
315-
println(it)
316308
/**
317309
* 5:VARINT 32
318310
* 3:LEN {"foo"}
@@ -371,15 +363,15 @@ class ProtobufOneOfTest {
371363
val buf = ProtoBuf { serializersModule = module }
372364

373365
val dataInt = OneOfData(IntType(42), "foo")
374-
val intString = buf.encodeToHexString(OneOfData.serializer(), dataInt).also { println(it) }
366+
val intString = buf.encodeToHexString(OneOfData.serializer(), dataInt)
375367
/**
376368
* 5:VARINT 42
377369
* 3:LEN {"foo"}
378370
*/
379371
assertEquals("282a1a03666f6f", intString)
380372

381373
val dataString = OneOfData(StringType("bar"), "foo")
382-
val stringString = buf.encodeToHexString(OneOfData.serializer(), dataString).also { println(it) }
374+
val stringString = buf.encodeToHexString(OneOfData.serializer(), dataString)
383375
/**
384376
* 6:LEN {"bar"}
385377
* 3:LEN {"foo"}
@@ -403,12 +395,16 @@ class ProtobufOneOfTest {
403395
@Test
404396
fun testFailWithClassEncoding() {
405397
val data = FailWithClass(FailIntType(42), "foo")
406-
assertFailsWith<IllegalArgumentException> { ProtoBuf.encodeToHexString(FailWithClass.serializer(), data) }
398+
assertFailsWithMessage<IllegalArgumentException>(
399+
"Implementation of oneOf type kotlinx.serialization.protobuf.ProtobufOneOfTest.FailIntType should have @ProtoNumber annotation"
400+
) { ProtoBuf.encodeToHexString(FailWithClass.serializer(), data) }
407401
}
408402

409403
@Test
410404
fun testFailWithClassDecoding() {
411-
assertFailsWith<MissingFieldException> {
405+
assertFailsWithMessage<IllegalArgumentException>(
406+
"kotlinx.serialization.protobuf.ProtobufOneOfTest.FailIntType implementing oneOf type kotlinx.serialization.protobuf.ProtobufOneOfTest.IFailType should have @ProtoNumber annotation in its single property."
407+
) {
412408
ProtoBuf.decodeFromHexString<FailWithClass>(
413409
/**
414410
* 5:VARINT 42
@@ -511,7 +507,9 @@ class ProtobufOneOfTest {
511507
assertEquals("1a03666f6f182a", it)
512508
}
513509

514-
assertFailsWith<IllegalArgumentException> {
510+
assertFailsWithMessage<IllegalArgumentException>(
511+
"Duplicated proto number 3 in kotlinx.serialization.protobuf.ProtobufOneOfTest.DuplicatingIdData for elements: d, bad."
512+
) {
515513
/**
516514
* 3:LEN {"foo"}
517515
* 3:VARINT 42
@@ -576,7 +574,6 @@ class ProtobufOneOfTest {
576574
fun testTypedInt() {
577575
val fixed = TypedIntOuter(Fixed32Int(32))
578576
ProtoBuf.encodeToHexString(fixed).also {
579-
println(it)
580577
/**
581578
* 2:I32 32i32
582579
*/
@@ -590,7 +587,6 @@ class ProtobufOneOfTest {
590587
}
591588
val fixedLong = TypedIntOuter(Fixed32Long(30576774159))
592589
ProtoBuf.encodeToHexString(fixedLong).also {
593-
println(it)
594590
/**
595591
* 3:VARINT 30576774159
596592
*/
@@ -604,7 +600,6 @@ class ProtobufOneOfTest {
604600
}
605601
val signed = TypedIntOuter(SignedInt(32))
606602
ProtoBuf.encodeToHexString(signed).also {
607-
println(it)
608603
/**
609604
* 4:VARINT 32
610605
*/
@@ -618,7 +613,6 @@ class ProtobufOneOfTest {
618613
}
619614
val signedLong = TypedIntOuter(SignedLong(30576774159))
620615
ProtoBuf.encodeToHexString(signedLong).also {
621-
println(it)
622616
/**
623617
* 5:VARINT 61153548318
624618
*/
@@ -632,7 +626,6 @@ class ProtobufOneOfTest {
632626
}
633627
val default = TypedIntOuter(DefaultInt(32))
634628
ProtoBuf.encodeToHexString(default).also {
635-
println(it)
636629
/**
637630
* 6:VARINT 32
638631
*/
@@ -646,7 +639,6 @@ class ProtobufOneOfTest {
646639
}
647640
val defaultLong = TypedIntOuter(DefaultLong(30576774159))
648641
ProtoBuf.encodeToHexString(defaultLong).also {
649-
println(it)
650642
/**
651643
* 7:VARINT 30576774159
652644
*/

formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/schema/SchemaValidationsTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class SchemaValidationsTest {
169169
assertContains(it, "oneof i")
170170
assertContains(it, "message InnerType")
171171
// oneof fields need no required keyword
172-
assertTrue(it.contains("required int32").not())
172+
assertFalse(it.contains("required int32"))
173173
}
174174

175175
assertFailsWithMessage<IllegalArgumentException>(

0 commit comments

Comments
 (0)