-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make serialization of Snowflake consistent (#316)
Snowflakes are decoded as Longs but encoded as Strings. Mixing these types is allowed in JSON (and is done by Discord quite frequently) but does lead to other encoding types failing. This commit changes snowflakes to be encoded and decoded as long values exclusively, and should fix the issue.
- Loading branch information
Showing
3 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package json | ||
|
||
import dev.kord.common.entity.Snowflake | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.decodeFromJsonElement | ||
import kotlinx.serialization.json.put | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class SnowflakeTest { | ||
|
||
@Serializable | ||
private data class SnowflakeContainer(val snowflake: Snowflake) | ||
|
||
@Test | ||
fun `Deserialization of Snowflake as String completes successfully`() { | ||
val value = "1337" | ||
val json = buildJsonObject { | ||
put("snowflake", value) | ||
} | ||
|
||
val container = Json.decodeFromJsonElement<SnowflakeContainer>(json) | ||
assertEquals(value, container.snowflake.asString) | ||
} | ||
|
||
@Test | ||
fun `Deserialization of Snowflake as Long completes successfully`() { | ||
val value = 1337L | ||
val json = buildJsonObject { | ||
put("snowflake", value) | ||
} | ||
|
||
val container = Json.decodeFromJsonElement<SnowflakeContainer>(json) | ||
assertEquals(value, container.snowflake.value) | ||
} | ||
|
||
@Test | ||
fun `Reserialization of Snowflake completes successfully`() { | ||
val json = buildJsonObject { put("snowflake", 1337L) } | ||
|
||
val container = Json.decodeFromJsonElement<SnowflakeContainer>(json) | ||
val reDecodedContainer = Json.decodeFromString<SnowflakeContainer>(Json.encodeToString(container)) | ||
assertEquals(container, reDecodedContainer) | ||
} | ||
|
||
} |