diff --git a/gateway/src/main/kotlin/DefaultGateway.kt b/gateway/src/main/kotlin/DefaultGateway.kt index 22c576ed796..1b122b18979 100644 --- a/gateway/src/main/kotlin/DefaultGateway.kt +++ b/gateway/src/main/kotlin/DefaultGateway.kt @@ -81,6 +81,7 @@ class DefaultGateway(private val data: DefaultGatewayData) : Gateway { private val jsonParser = Json { ignoreUnknownKeys = true + isLenient = true } private val stateMutex = Mutex() diff --git a/gateway/src/main/kotlin/Event.kt b/gateway/src/main/kotlin/Event.kt index 839922b82e2..539dae5e62f 100644 --- a/gateway/src/main/kotlin/Event.kt +++ b/gateway/src/main/kotlin/Event.kt @@ -95,9 +95,16 @@ sealed class Event { } + @OptIn(ExperimentalSerializationApi::class) private fun getByDispatchEvent(index: Int, decoder: CompositeDecoder, name: String?, sequence: Int?) = when (name) { - "PRESENCES_REPLACE" -> null //https://github.com/kordlib/kord/issues/42 - "RESUMED" -> Resumed(sequence) + "PRESENCES_REPLACE" -> { + decoder.decodeNullableSerializableElement(descriptor, index, JsonElement.serializer().nullable) + null //https://github.com/kordlib/kord/issues/42 + } + "RESUMED" -> { + decoder.decodeNullableSerializableElement(descriptor, index, JsonElement.serializer().nullable) + Resumed(sequence) + } "READY" -> Ready(decoder.decodeSerializableElement(descriptor, index, ReadyData.serializer()), sequence) "CHANNEL_CREATE" -> ChannelCreate(decoder.decodeSerializableElement(descriptor, index, DiscordChannel.serializer()), sequence) "CHANNEL_UPDATE" -> ChannelUpdate(decoder.decodeSerializableElement(descriptor, index, DiscordChannel.serializer()), sequence) diff --git a/gateway/src/test/kotlin/json/RegressionTests.kt b/gateway/src/test/kotlin/json/RegressionTests.kt index b5daac546b0..beef1f9b47a 100644 --- a/gateway/src/test/kotlin/json/RegressionTests.kt +++ b/gateway/src/test/kotlin/json/RegressionTests.kt @@ -18,4 +18,19 @@ class RegressionTests { event shouldBe Reconnect } -} \ No newline at end of file + @Test + fun `Resumed with unknown data`(){ + Json.decodeFromString(Event.Companion, file("resumeWithUnknownData")) + } + + @Test + fun `PresenceReplace with unknown data`(){ + Json.decodeFromString(Event.Companion, file("presenceReplaceWithUnknownData")) + } + + @Test + fun `Unknown event with successfully parses`(){ + Json.decodeFromString(Event.Companion, file("eventWithUnknownData")) //dispatch event with non-existent type + } + +} diff --git a/gateway/src/test/resources/json/regression/eventWithUnknownData.json b/gateway/src/test/resources/json/regression/eventWithUnknownData.json new file mode 100644 index 00000000000..631b74e54bb --- /dev/null +++ b/gateway/src/test/resources/json/regression/eventWithUnknownData.json @@ -0,0 +1,6 @@ +{ + "t": "DOCUMENTED_API", + "s": 30, + "op": 0, + "d": {} +} diff --git a/gateway/src/test/resources/json/regression/presenceReplaceWithUnknownData.json b/gateway/src/test/resources/json/regression/presenceReplaceWithUnknownData.json new file mode 100644 index 00000000000..fe74cb116a4 --- /dev/null +++ b/gateway/src/test/resources/json/regression/presenceReplaceWithUnknownData.json @@ -0,0 +1,8 @@ +{ + "t": "PRESENCES_REPLACE", + "s": 1337, + "op": 0, + "d": { + "some" : "key" + } +} diff --git a/gateway/src/test/resources/json/regression/resumeWithUnknownData.json b/gateway/src/test/resources/json/regression/resumeWithUnknownData.json new file mode 100644 index 00000000000..5300e3999b8 --- /dev/null +++ b/gateway/src/test/resources/json/regression/resumeWithUnknownData.json @@ -0,0 +1,10 @@ +{ + "t": "RESUMED", + "s": 30, + "op": 0, + "d": { + "_trace": [ + "[\"gateway-prd-main-dj3z\",{\"micros\":1542,\"calls\":[\"discord-sessions-prd-2-122\",{\"micros\":16}]}]" + ] + } +}