Skip to content

fix: replace unsafe Message.Bytes cast with exhaustive when expression#102

Open
dhiraj-143r wants to merge 1 commit intogojek:mainfrom
dhiraj-143r:fix/issue-101-unsafe-message-bytes-cast
Open

fix: replace unsafe Message.Bytes cast with exhaustive when expression#102
dhiraj-143r wants to merge 1 commit intogojek:mainfrom
dhiraj-143r:fix/issue-101-unsafe-message-bytes-cast

Conversation

@dhiraj-143r
Copy link
Copy Markdown

Fixes #101

The send() method in MqttCourierClient does a hard cast (message as Message.Bytes).value which is unsafe since Message is a sealed class meant for extension. A custom MessageAdapter could return a different subtype, causing a ClassCastException.

Added a toByteArray() method to the Message sealed class using an exhaustive when, and replaced the hard cast with it. This is the same pattern already used in GsonMessageAdapter.fromMessage().

Replace the hard cast '(message as Message.Bytes).value' with a new
toByteArray() method on the Message sealed class that uses an exhaustive
when expression. This prevents ClassCastException if new Message subtypes
are added in the future.

Fixes gojek#101
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.

Bug: Crash in MqttCourierClient.send() with non-Bytes messages

2 participants