Skip to content

Commit c225811

Browse files
Checking one of element with 2 properties should fail
1 parent c5f0503 commit c225811

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

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

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,65 @@ class ProtobufOneOfTest {
217217
}
218218

219219
@Serializable
220-
data class FailType(@ProtoNumber(8) val i: Int, @ProtoNumber(9) val j: Int) : IType
220+
data class FailOuter(@ProtoOneOf val i: IFailSuper, @ProtoNumber(3) val name: String)
221+
222+
@Serializable
223+
data class FailOuterHelper(
224+
@ProtoNumber(5) val i: Int,
225+
@ProtoNumber(6) val j: Int,
226+
@ProtoNumber(3) val name: String
227+
)
228+
229+
@Serializable
230+
sealed interface IFailSuper
231+
232+
@Serializable
233+
data class FailType(@ProtoNumber(5) val i: Int, @ProtoNumber(6) val j: Int) : IFailSuper
221234

222235
@Test
223236
fun testOneOfElementCheck() {
224-
val data = OneOfData(FailType(1, 2), "foo")
237+
val data = FailOuter(FailType(1, 2), "foo")
225238
assertFailsWithMessage<IllegalArgumentException>(
226239
message = "Implementation of oneOf type" +
227240
" kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
228241
" should contain only 1 element, but get 2"
229242
) {
230-
ProtoBuf.encodeToHexString(OneOfData.serializer(), data)
243+
ProtoBuf.encodeToHexString(data)
231244
}
245+
246+
/**
247+
* 5:VARINT 42
248+
* 3:LEN {"foo"}
249+
*/
250+
assertFailsWithMessage<IllegalArgumentException>(
251+
message = "Implementation of oneOf type" +
252+
" kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
253+
" should contain only 1 element, but get 2"
254+
) {
255+
ProtoBuf.decodeFromHexString<FailOuter>("282a1a03666f6f")
256+
}
257+
258+
/**
259+
* 5:VARINT 1
260+
* 6:VARINT 2
261+
* 3:LEN {"foo"}
262+
*/
263+
assertFailsWithMessage<IllegalArgumentException>(
264+
message = "Implementation of oneOf type" +
265+
" kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType" +
266+
" should contain only 1 element, but get 2"
267+
) {
268+
ProtoBuf.decodeFromHexString<FailOuter>(
269+
ProtoBuf.encodeToHexString(
270+
FailOuterHelper(
271+
i = 1,
272+
j = 2,
273+
name = "foo"
274+
)
275+
)
276+
)
277+
}
278+
232279
}
233280

234281
@Serializable

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class SchemaValidationsTest {
175175
assertFailsWithMessage<IllegalArgumentException>(
176176
message = "Implementation of oneOf type kotlinx.serialization.protobuf.ProtobufOneOfTest.FailType should contain only 1 element, but get 2"
177177
) {
178-
ProtoBufSchemaGenerator.generateSchemaText(ProtobufOneOfTest.OneOfDataNullable.serializer().descriptor)
178+
ProtoBufSchemaGenerator.generateSchemaText(ProtobufOneOfTest.FailOuter.serializer().descriptor)
179179
}
180180
}
181181
}

0 commit comments

Comments
 (0)