Skip to content
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

Fix negative valued enums deserializing for protobuf #2400

Merged
merged 1 commit into from Aug 14, 2023

Conversation

Dogacel
Copy link
Contributor

@Dogacel Dogacel commented Aug 5, 2023

From language guide:

Enumerator constants must be in the range of a 32-bit integer. Since enum values use varint encoding on the wire, negative values are inefficient and thus not recommended

Previously, I had the following error when trying to deserialize:

java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 4
	at kotlinx.serialization.internal.PluginGeneratedSerialDescriptor.getElementAnnotations(PluginGeneratedSerialDescriptor.kt:137)
	at kotlinx.serialization.protobuf.internal.HelpersKt.extractProtoId(Helpers.kt:74)
	at kotlinx.serialization.protobuf.internal.ProtobufDecoder.findIndexByTag(ProtobufDecoding.kt:84)
	at kotlinx.serialization.protobuf.internal.ProtobufDecoder.decodeTaggedEnum(ProtobufDecoding.kt:183)
	at kotlinx.serialization.protobuf.internal.ProtobufTaggedDecoder.decodeEnum(ProtobufTaggedDecoder.kt:40)

Added a test case to verify that solution works.

Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

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

Thanks!

Note: It seems it will also make possible to use negative numbers for regular proto fields (which is not allowed per spec), but we can always prohibit it later.

@sandwwraith sandwwraith merged commit a7109d8 into Kotlin:dev Aug 14, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants