Skip to content

Commit

Permalink
Add test for DataClassWithJsonElement
Browse files Browse the repository at this point in the history
Signed-off-by: Mark <mark.vogel@nomadgcs.com>
  • Loading branch information
markvogelnomad committed Nov 8, 2023
1 parent dc6b920 commit fb0a3ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import kotlin.test.assertEquals
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MissingFieldException
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.plus
import kotlinx.serialization.modules.polymorphic
Expand Down Expand Up @@ -71,6 +73,7 @@ import org.bson.codecs.kotlinx.samples.DataClassWithEncodeDefault
import org.bson.codecs.kotlinx.samples.DataClassWithEnum
import org.bson.codecs.kotlinx.samples.DataClassWithEnumMapKey
import org.bson.codecs.kotlinx.samples.DataClassWithFailingInit
import org.bson.codecs.kotlinx.samples.DataClassWithJsonElement
import org.bson.codecs.kotlinx.samples.DataClassWithMutableList
import org.bson.codecs.kotlinx.samples.DataClassWithMutableMap
import org.bson.codecs.kotlinx.samples.DataClassWithMutableSet
Expand Down Expand Up @@ -129,6 +132,46 @@ class KotlinSerializerCodecTest {

private val allBsonTypesDocument = BsonDocument.parse(allBsonTypesJson)

@Test
fun testDataClassWithJsonElement() {

/*
* We need to encode all integer values as longs because the JsonElementSerializer
* doesn't actually use our JsonEncoder instead it uses an inferior
* JsonPrimitiveSerializer and ignores ours altogether encoding all integers as longs
*
* On the other hand, BsonDocument decodes everything as integers unless it's explicitly
* set as a long, and therefore we get a type mismatch
*/

val expected = """{"value": {
|"char": "c",
|"byte": {"$numberLong": "0"},
|"short": {"$numberLong": "1"},
|"int": {"$numberLong": "22"},
|"long": {"$numberLong": "42"},
|"float": 4.0,
|"double": 4.2,
|"boolean": true,
|"string": "String"
|}}""".trimMargin()
val dataClass = DataClassWithJsonElement(
buildJsonObject {
put("char", "c")
put("byte", 0)
put("short", 1)
put("int", 22)
put("long", 42L)
put("float", 4.0)
put("double", 4.2)
put("boolean", true)
put("string", "String")
}
)

assertRoundTrips(expected, dataClass)
}

@Test
fun testDataClassWithSimpleValues() {
val expected =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Required
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import org.bson.BsonArray
import org.bson.BsonBinary
import org.bson.BsonBoolean
Expand Down Expand Up @@ -50,6 +51,11 @@ import org.bson.codecs.pojo.annotations.BsonProperty
import org.bson.codecs.pojo.annotations.BsonRepresentation
import org.bson.types.ObjectId

@Serializable
data class DataClassWithJsonElement(
val value: JsonElement
)

@Serializable
data class DataClassWithSimpleValues(
val char: Char,
Expand Down

0 comments on commit fb0a3ab

Please sign in to comment.