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

Port kotlinx.serialization to 1.2.0 #279

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading