Skip to content

Implemented encoding null in key and value of a map in Protobuf #2910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal abstract class ProtobufTaggedEncoder : ProtobufTaggedBase(), Encoder, C
ACCEPTABLE,
OPTIONAL,
COLLECTION,
LIST_ELEMENT,
NOT_NULL
}
private var nullableMode: NullableMode = NullableMode.NOT_NULL
Expand All @@ -37,7 +38,8 @@ internal abstract class ProtobufTaggedEncoder : ProtobufTaggedBase(), Encoder, C
if (nullableMode != NullableMode.ACCEPTABLE) {
val message = when (nullableMode) {
NullableMode.OPTIONAL -> "'null' is not supported for optional properties in ProtoBuf"
NullableMode.COLLECTION -> "'null' is not supported for collection types in ProtoBuf"
NullableMode.COLLECTION -> "'null' is not supported as the value of collection types in ProtoBuf"
NullableMode.LIST_ELEMENT -> "'null' is not supported as the value of a list element in ProtoBuf"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is LIST_ELEMENT added for the purpose of proper error message only? I do not see this string being tested anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, only the accepted or not flag is sufficient to throw an error. The enumeration is created only for printong different messages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then add a test for this message pls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added

NullableMode.NOT_NULL -> "'null' is not allowed for not-null properties"
else -> "'null' is not supported in ProtoBuf";
}
Expand Down Expand Up @@ -137,12 +139,12 @@ internal abstract class ProtobufTaggedEncoder : ProtobufTaggedBase(), Encoder, C
NullableMode.OPTIONAL
else {
val elementDescriptor = descriptor.getElementDescriptor(index)
if (elementDescriptor.kind.isMapOrList())
NullableMode.COLLECTION
else if (!descriptor.kind.isMapOrList() && elementDescriptor.isNullable) // or: `serializer.descriptor`
NullableMode.ACCEPTABLE
else
NullableMode.NOT_NULL
when {
!elementDescriptor.isNullable -> NullableMode.NOT_NULL
elementDescriptor.kind.isMapOrList() -> NullableMode.COLLECTION
descriptor.kind == StructureKind.LIST -> NullableMode.LIST_ELEMENT
else -> NullableMode.ACCEPTABLE
}
}

pushTag(descriptor.getTag(index))
Expand All @@ -157,10 +159,15 @@ internal abstract class ProtobufTaggedEncoder : ProtobufTaggedBase(), Encoder, C
) {
nullableMode = if (descriptor.isElementOptional(index))
NullableMode.OPTIONAL
else if (descriptor.getElementDescriptor(index).kind.isMapOrList())
NullableMode.COLLECTION
else
NullableMode.ACCEPTABLE
else {
// we don't check nullability of element because this function called always for nullable `value`
val elementDescriptor = descriptor.getElementDescriptor(index)
when {
elementDescriptor.kind.isMapOrList() -> NullableMode.COLLECTION
descriptor.kind == StructureKind.LIST -> NullableMode.LIST_ELEMENT
else -> NullableMode.ACCEPTABLE
}
}

pushTag(descriptor.getTag(index))
encodeNullableSerializableValue(serializer, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@ class ProtobufCollectionsTest {
@Serializable
data class MapWithNullableNestedLists(val m: Map<List<Int>?, List<Int>?>)

@Serializable
data class MapWithNullableNestedMaps(val m: Map<Map<String, Int>?, Map<String, Int>?>)

@Serializable
data class NullableListElement(val l: List<Int?>)

@Serializable
data class NullableMapKey(val m: Map<Int?, Int>)

@Serializable
data class NullableMapValue(val m: Map<Int, Int?>)
data class NullableMap(val m: Map<Int?, Int?>)

@Test
fun testEncodeNullAsListElement() {
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(NullableListElement(listOf(null))) }
}

@Test
fun testEncodeNullAsMapKey() {
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(NullableMapKey(mapOf(null to 42))) }
}

@Test
fun testEmptyListIsNotListOfEmpty() {
val emptyListBytes = ProtoBuf.encodeToByteArray(ListWithNestedList(emptyList()))
Expand All @@ -54,15 +52,46 @@ class ProtobufCollectionsTest {
}

@Test
fun testEncodeNullAsMapValue() {
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(NullableMapValue(mapOf(42 to null))) }
fun testNullMap() {
val keyNull = NullableMap(mapOf(null to 42))
val valueNull = NullableMap(mapOf(42 to null))
val bothNull = NullableMap(mapOf(null to null))

val encodedKeyNull = ProtoBuf.encodeToHexString(keyNull)
val encodedValueNull = ProtoBuf.encodeToHexString(valueNull)
val encodedBothNull = ProtoBuf.encodeToHexString(bothNull)
assertEquals(encodedKeyNull, "0a02102a")
assertEquals(encodedValueNull, "0a02082a")
assertEquals(encodedBothNull, "0a00")

val decodedKeyNull = ProtoBuf.decodeFromHexString<NullableMap>(encodedKeyNull)
val decodedValueNull = ProtoBuf.decodeFromHexString<NullableMap>(encodedValueNull)
val decodedBothNull = ProtoBuf.decodeFromHexString<NullableMap>(encodedBothNull)
assertEquals(decodedKeyNull, keyNull)
assertEquals(decodedValueNull, valueNull)
assertEquals(decodedBothNull, bothNull)
}

@Test
fun testRepeatNullKeyInMap() {
// there are two entries in message: (null to 42) and (null to 43), the last one is used
val decoded = ProtoBuf.decodeFromHexString<NullableMap>("0a04102a102b")
assertEquals(NullableMap(mapOf(null to 43)), decoded)
}

@Test
fun testCollectionsInNullableMap() {
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(MapWithNullableNestedLists(mapOf(null to listOf(42))) ) }
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(MapWithNullableNestedLists(mapOf(listOf(42) to null)) ) }
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(MapWithNullableNestedMaps(mapOf(null to mapOf("key" to 42))) ) }
assertFailsWith(SerializationException::class) { ProtoBuf.encodeToByteArray(MapWithNullableNestedMaps(mapOf(mapOf("key" to 42) to null)) ) }
}

@Test
fun testEncodeMapWithNullableValue() {
val map = NullableMapValue(mapOf(42 to 43))
val map = NullableMap(mapOf(42 to 43))
val bytes = ProtoBuf.encodeToByteArray(map)
val decoded = ProtoBuf.decodeFromByteArray<NullableMapValue>(bytes)
val decoded = ProtoBuf.decodeFromByteArray<NullableMap>(bytes)
assertEquals(map, decoded)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ProtobufTypeParameterTest {
fail()
} catch (e: SerializationException) {
assertEquals(
"'null' is not supported for collection types in ProtoBuf", e.message
"'null' is not supported as the value of collection types in ProtoBuf", e.message
)
}
}
Expand Down