Skip to content

Commit fb0a3ab

Browse files
Add test for DataClassWithJsonElement
Signed-off-by: Mark <[email protected]>
1 parent dc6b920 commit fb0a3ab

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/KotlinSerializerCodecTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import kotlin.test.assertEquals
1919
import kotlinx.serialization.ExperimentalSerializationApi
2020
import kotlinx.serialization.MissingFieldException
2121
import kotlinx.serialization.SerializationException
22+
import kotlinx.serialization.json.buildJsonObject
23+
import kotlinx.serialization.json.put
2224
import kotlinx.serialization.modules.SerializersModule
2325
import kotlinx.serialization.modules.plus
2426
import kotlinx.serialization.modules.polymorphic
@@ -71,6 +73,7 @@ import org.bson.codecs.kotlinx.samples.DataClassWithEncodeDefault
7173
import org.bson.codecs.kotlinx.samples.DataClassWithEnum
7274
import org.bson.codecs.kotlinx.samples.DataClassWithEnumMapKey
7375
import org.bson.codecs.kotlinx.samples.DataClassWithFailingInit
76+
import org.bson.codecs.kotlinx.samples.DataClassWithJsonElement
7477
import org.bson.codecs.kotlinx.samples.DataClassWithMutableList
7578
import org.bson.codecs.kotlinx.samples.DataClassWithMutableMap
7679
import org.bson.codecs.kotlinx.samples.DataClassWithMutableSet
@@ -129,6 +132,46 @@ class KotlinSerializerCodecTest {
129132

130133
private val allBsonTypesDocument = BsonDocument.parse(allBsonTypesJson)
131134

135+
@Test
136+
fun testDataClassWithJsonElement() {
137+
138+
/*
139+
* We need to encode all integer values as longs because the JsonElementSerializer
140+
* doesn't actually use our JsonEncoder instead it uses an inferior
141+
* JsonPrimitiveSerializer and ignores ours altogether encoding all integers as longs
142+
*
143+
* On the other hand, BsonDocument decodes everything as integers unless it's explicitly
144+
* set as a long, and therefore we get a type mismatch
145+
*/
146+
147+
val expected = """{"value": {
148+
|"char": "c",
149+
|"byte": {"$numberLong": "0"},
150+
|"short": {"$numberLong": "1"},
151+
|"int": {"$numberLong": "22"},
152+
|"long": {"$numberLong": "42"},
153+
|"float": 4.0,
154+
|"double": 4.2,
155+
|"boolean": true,
156+
|"string": "String"
157+
|}}""".trimMargin()
158+
val dataClass = DataClassWithJsonElement(
159+
buildJsonObject {
160+
put("char", "c")
161+
put("byte", 0)
162+
put("short", 1)
163+
put("int", 22)
164+
put("long", 42L)
165+
put("float", 4.0)
166+
put("double", 4.2)
167+
put("boolean", true)
168+
put("string", "String")
169+
}
170+
)
171+
172+
assertRoundTrips(expected, dataClass)
173+
}
174+
132175
@Test
133176
fun testDataClassWithSimpleValues() {
134177
val expected =

bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/samples/DataClasses.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
2121
import kotlinx.serialization.Required
2222
import kotlinx.serialization.SerialName
2323
import kotlinx.serialization.Serializable
24+
import kotlinx.serialization.json.JsonElement
2425
import org.bson.BsonArray
2526
import org.bson.BsonBinary
2627
import org.bson.BsonBoolean
@@ -50,6 +51,11 @@ import org.bson.codecs.pojo.annotations.BsonProperty
5051
import org.bson.codecs.pojo.annotations.BsonRepresentation
5152
import org.bson.types.ObjectId
5253

54+
@Serializable
55+
data class DataClassWithJsonElement(
56+
val value: JsonElement
57+
)
58+
5359
@Serializable
5460
data class DataClassWithSimpleValues(
5561
val char: Char,

0 commit comments

Comments
 (0)