You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our backend server in go may assign the map as key to null and send data like 0a050a036b6579 (1: {1: {"key"}} in readable string) with value absent.
But I found it impossible to define a valid class in represent to this message.
I tried:
@Serializable
data classProtoMap(@ProtoNumber(1) valmap:Map<String, TypeValue>)
to see if parser will give a default 0 in map, but failed to parse, and
@Serializable
data classProtoMapNullable(@ProtoNumber(1) valmap:Map<String, TypeValue?>)
This is good for decoding, but failing in encoding.
Is there any suggestion for such message, or it is a bug of map encoding?
PS I believe it is valid data to have null value in map, as described in Proto Spec.
To Reproduce
Run the test code below
packagekotlinx.serializationimportkotlinx.serialization.protobuf.ProtoBufimportkotlinx.serialization.protobuf.ProtoNumberimportkotlin.test.Testimportkotlin.test.assertEquals
@Serializable
data classTypeValue(@ProtoNumber(1) valint:Int = 0)
@Serializable
data classProtoMap(@ProtoNumber(1) valmap:Map<String, TypeValue>)
@Serializable
data classProtoMapNullable(@ProtoNumber(1) valmap:Map<String, TypeValue?>)
constval hexWithoutValue ="0a050a036b6579"classMapTest {
@Test
funtestDecodeNullValueToNullableMap() {
val value = hexWithoutValue
val decoded =ProtoBuf.decodeFromHexString<ProtoMapNullable>(value)
assertEquals(ProtoMapNullable(mapOf("key" to null)), decoded)
val encode =ProtoBuf.encodeToHexString(decoded)
println(encode)
}
@Test
funtestDecodeNullValueToMap() {
val value = hexWithoutValue
val decoded =ProtoBuf.decodeFromHexString<ProtoMap>(value)
assertEquals(ProtoMap(mapOf("key" to TypeValue(0))), decoded)
val encode =ProtoBuf.encodeToHexString(decoded)
println(encode)
}
}
Expected behavior
One of the test should be passed, maybe?
Environment
Kotlin version: 2.0.0 and 1.9.23
Library version: 1.7.1 and 1.6.3
Kotlin platforms: JVM, JS, iOS
The text was updated successfully, but these errors were encountered:
Describe the bug
Giving a simple proto message:
Our backend server in go may assign the map as
key to null
and send data like0a050a036b6579
(1: {1: {"key"}}
in readable string) with value absent.But I found it impossible to define a valid class in represent to this message.
I tried:
to see if parser will give a default 0 in map, but failed to parse, and
This is good for decoding, but failing in encoding.
Is there any suggestion for such message, or it is a bug of map encoding?
PS I believe it is valid data to have null value in map, as described in Proto Spec.
To Reproduce
Run the test code below
Expected behavior
One of the test should be passed, maybe?
Environment
The text was updated successfully, but these errors were encountered: