Skip to content

Commit

Permalink
Port kotlinx.serialization to 1.2.0
Browse files Browse the repository at this point in the history
- Convert local classes to top level classes (See Kotlin/kotlinx.serialization#1472)
- Improve handling of empty JSON bodies (See Kotlin/kotlinx.serialization#678)
- Fix Failing Command test
  • Loading branch information
DRSchlaubi committed May 9, 2021
1 parent 4a2f7f1 commit 54674be
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 132 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object Versions {
const val kotlin = "1.5.0"
const val kotlinxSerialization = "1.0.0"
const val kotlinxSerialization = "1.2.0"
const val ktor = "1.5.3"
const val kotlinxCoroutines = "1.5.0-RC"
const val kotlinLogging = "2.0.4"
Expand Down
23 changes: 12 additions & 11 deletions common/src/test/kotlin/entity/optional/OptionalBooleanTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,42 @@ import org.junit.jupiter.api.Test

internal class OptionalBooleanTest {

@Serializable
private class EmptyOptionalEntity(val value: OptionalBoolean = OptionalBoolean.Missing)

@Test
fun `deserializing nothing in optional assigns Missing`(){
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: OptionalBoolean = OptionalBoolean.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<EmptyOptionalEntity>(json)

assert(entity.value is OptionalBoolean.Missing)
}

@Serializable
private class NullOptionalEntity(@Suppress("unused") val value: OptionalBoolean = OptionalBoolean.Missing)

@Test
fun `deserializing null in optional throws SerializationException`(){
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalBoolean = OptionalBoolean.Missing)

org.junit.jupiter.api.assertThrows<SerializationException> {
Json.decodeFromString<Entity>(json)
Json.decodeFromString<NullOptionalEntity>(json)
}
}

@Serializable
private class ValueOptionalEntity(@Suppress("unused") val value: OptionalBoolean = OptionalBoolean.Missing)

@Test
fun `deserializing value in optional assigns Value`(){
@Language("json")
val json = """{ "value":true }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalBoolean = OptionalBoolean.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<ValueOptionalEntity>(json)
require(entity.value is OptionalBoolean.Value)

Assertions.assertEquals(true, entity.value.value)
Expand Down
24 changes: 13 additions & 11 deletions common/src/test/kotlin/entity/optional/OptionalIntTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,43 @@ import org.junit.jupiter.api.Test

internal class OptionalIntTest {

@Serializable
private class EmptyOptionalEntity(val value: OptionalInt = OptionalInt.Missing)

@Test
fun `deserializing nothing in optional assigns Missing`(){
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: OptionalInt = OptionalInt.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<EmptyOptionalEntity>(json)

assert(entity.value is OptionalInt.Missing)
}


@Serializable
private class NullOptionalEntity(@Suppress("unused") val value: OptionalInt = OptionalInt.Missing)

@Test
fun `deserializing null in optional throws SerializationException`(){
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalInt = OptionalInt.Missing)

org.junit.jupiter.api.assertThrows<SerializationException> {
Json.decodeFromString<Entity>(json)
Json.decodeFromString<NullOptionalEntity>(json)
}
}

@Serializable
class ValueOptionalEntity(@Suppress("unused") val value: OptionalInt = OptionalInt.Missing)

@Test
fun `deserializing value in optional assigns Value`(){
@Language("json")
val json = """{ "value":5 }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalInt = OptionalInt.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<ValueOptionalEntity>(json)
require(entity.value is OptionalInt.Value)

assertEquals(5, entity.value.value)
Expand Down
30 changes: 17 additions & 13 deletions common/src/test/kotlin/entity/optional/OptionalLongTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,45 @@ import org.junit.jupiter.api.Test

internal class OptionalLongTest {

@Serializable
class EmptyOptionalEntity(val value: OptionalLong = OptionalLong.Missing)

@Test
fun `deserializing nothing in optional assigns Missing`(){
fun `deserializing nothing in optional assigns Missing`() {
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: OptionalLong = OptionalLong.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<EmptyOptionalEntity>(json)

assert(entity.value is OptionalLong.Missing)
}


@Serializable
class NullOptionalEntity(@Suppress("unused") val value: OptionalLong = OptionalLong.Missing)

@Test
fun `deserializing null in optional throws SerializationException`(){
fun `deserializing null in optional throws SerializationException`() {
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalLong = OptionalLong.Missing)

org.junit.jupiter.api.assertThrows<SerializationException> {
Json.decodeFromString<Entity>(json)
Json.decodeFromString<NullOptionalEntity>(json)
}
}


@Serializable
class ValueOptionalEntity(@Suppress("unused") val value: OptionalLong = OptionalLong.Missing)

@Test
fun `deserializing value in optional assigns Value`(){
fun `deserializing value in optional assigns Value`() {
@Language("json")
val json = """{ "value":5 }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalLong = OptionalLong.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<ValueOptionalEntity>(json)
require(entity.value is OptionalLong.Value)

Assertions.assertEquals(5, entity.value.value)
Expand Down
26 changes: 15 additions & 11 deletions common/src/test/kotlin/entity/optional/OptionalSnowflakeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,45 @@ import org.junit.jupiter.api.Test

internal class OptionalSnowflakeTest {


@Serializable
class EmptyOptionalEntity(val value: OptionalSnowflake = OptionalSnowflake.Missing)

@Test
fun `deserializing nothing in optional assigns Missing`(){
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: OptionalSnowflake = OptionalSnowflake.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<EmptyOptionalEntity>(json)

assert(entity.value is OptionalSnowflake.Missing)
}


@Serializable
class NullOptionalEntity(@Suppress("unused") val value: OptionalSnowflake = OptionalSnowflake.Missing)

@Test
fun `deserializing null in optional throws SerializationException`(){
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalSnowflake = OptionalSnowflake.Missing)

org.junit.jupiter.api.assertThrows<SerializationException> {
Json.decodeFromString<Entity>(json)
Json.decodeFromString<NullOptionalEntity>(json)
}
}


@Serializable
class ValueOptionalEntity(@Suppress("unused") val value: OptionalSnowflake = OptionalSnowflake.Missing)

@Test
fun `deserializing value in optional assigns Value`(){
@Language("json")
val json = """{ "value":5 }"""

@Serializable
class Entity(@Suppress("unused") val value: OptionalSnowflake = OptionalSnowflake.Missing)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<ValueOptionalEntity>(json)
require(entity.value is OptionalSnowflake.Value)

Assertions.assertEquals(Snowflake(5), entity.value.value)
Expand Down
48 changes: 26 additions & 22 deletions common/src/test/kotlin/entity/optional/OptionalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Test
internal class OptionalTest {

@Test
fun `creating optional from nullable value returns Value on non-null value`(){
fun `creating optional from nullable value returns Value on non-null value`() {
val value: Int? = 5
val optional = Optional(value)

Expand All @@ -19,62 +19,66 @@ internal class OptionalTest {
}

@Test
fun `creating optional from nullable value returns Null on null value`(){
fun `creating optional from nullable value returns Null on null value`() {
val value: Int? = null
val optional = Optional(value)

assert(optional is Optional.Null)
}


@Serializable
private class NullOptionalEntity(val value: Optional<String?>)

@Test
fun `deserializing null in nullable optional assigns Null`(){
fun `deserializing null in nullable optional assigns Null`() {
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(val value: Optional<String?>)

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<NullOptionalEntity>(json)

assert(entity.value is Optional.Null)
}


@Serializable
class EmptyOptionalEntity(val value: Optional<String?> = Optional.Missing())

@Test
fun `deserializing nothing in nullable optional assigns Missing`(){
fun `deserializing nothing in nullable optional assigns Missing`() {
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: Optional<String?> = Optional.Missing())

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<EmptyOptionalEntity>(json)

assert(entity.value is Optional.Missing)
}


@Serializable
class UnexpectedEmptyOptionalEntity(val value: Optional<String> = Optional.Missing())

@Test
fun `deserializing nothing in non-nullable optional assigns Missing`(){
fun `deserializing nothing in non-nullable optional assigns Missing`() {
@Language("json")
val json = """{}"""

@Serializable
class Entity(val value: Optional<String> = Optional.Missing())

val entity = Json.decodeFromString<Entity>(json)
val entity = Json.decodeFromString<UnexpectedEmptyOptionalEntity>(json)

assert(entity.value is Optional.Missing)
}


@Serializable
private class UnexpectedNullOptionalEntity(@Suppress("unused") val value: Optional<String> = Optional.Missing())

@Test
fun `deserializing null in non-nullable optional throws SerializationException`(){
fun `deserializing null in non-nullable optional throws SerializationException`() {
@Language("json")
val json = """{ "value":null }"""

@Serializable
class Entity(@Suppress("unused") val value: Optional<String> = Optional.Missing())

org.junit.jupiter.api.assertThrows<SerializationException> {
Json.decodeFromString<Entity>(json)
Json.decodeFromString<UnexpectedNullOptionalEntity>(json)
}
}

Expand Down
4 changes: 1 addition & 3 deletions core/src/test/kotlin/regression/CacheMissRegression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ class CrashingHandler(val client: HttpClient) : RequestHandler {

}.execute()

return parser.decodeFromString(request.route.strategy, response.readText())


return request.route.mapper.deserialize(parser, response.readText())
}
}

Expand Down
24 changes: 19 additions & 5 deletions gateway/src/test/kotlin/json/CommandTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class CommandTest {
val query = "test"
val limit = 1337

val request = json.encodeToString(Command.Companion, RequestGuildMembers(Snowflake(guildId), query.optional(), OptionalInt.Value(limit)))
val request = json.encodeToString(
Command.Companion,
RequestGuildMembers(Snowflake(guildId), query.optional(), OptionalInt.Value(limit))
)

val json = json.encodeToString(JsonObject.serializer(), buildJsonObject {
put("op", OpCode.RequestGuildMembers.code)
Expand All @@ -83,7 +86,10 @@ class CommandTest {
val selfMute = true
val selfDeaf = false

val status = json.encodeToString(Command.Companion, UpdateVoiceStatus(Snowflake(guildId), Snowflake(channelId), selfMute, selfDeaf))
val status = json.encodeToString(
Command.Companion,
UpdateVoiceStatus(Snowflake(guildId), Snowflake(channelId), selfMute, selfDeaf)
)

val json = json.encodeToString(JsonObject.serializer(), buildJsonObject {
put("op", OpCode.VoiceStateUpdate.code)
Expand Down Expand Up @@ -112,7 +118,7 @@ class CommandTest {
put("op", OpCode.StatusUpdate.code)
put("d", buildJsonObject {
put("since", since)
put("activities", null as String?)
put("activities", buildJsonArray {})
put("status", status.value.lowercase(Locale.getDefault()))
put("afk", afk)
})
Expand All @@ -133,8 +139,16 @@ class CommandTest {
val presence: DiscordPresence? = null

val identify = json.encodeToString(
Command.Companion,
Identify(token, properties, compress.optional(), largeThreshold.optionalInt(), shard.optional(), presence.optional().coerceToMissing(), Intents.all)
Command.Companion,
Identify(
token,
properties,
compress.optional(),
largeThreshold.optionalInt(),
shard.optional(),
presence.optional().coerceToMissing(),
Intents.all
)
)

val json = json.encodeToString(JsonObject.serializer(), buildJsonObject {
Expand Down
Loading

0 comments on commit 54674be

Please sign in to comment.