Skip to content

Commit c055a07

Browse files
authored
Merge pull request #271 from ProjectMapK/porting-tests
Porting tests
2 parents 19d5d3d + d2af3bf commit c055a07

File tree

6 files changed

+234
-13
lines changed

6 files changed

+234
-13
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.databind.MapperFeature
4+
import io.github.projectmapk.jackson.module.kogera.jsonMapper
5+
import io.github.projectmapk.jackson.module.kogera.kotlinModule
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class GitHub314 {
10+
// Since Nothing? is compiled as a Void, it can be serialized by specifying ALLOW_VOID_VALUED_PROPERTIES
11+
data object NothingData {
12+
val data: Nothing? = null
13+
}
14+
15+
@Test
16+
fun test() {
17+
val expected = """{"data":null}"""
18+
19+
val withoutKotlinModule = jsonMapper { enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) }
20+
assertEquals(expected, withoutKotlinModule.writeValueAsString(NothingData))
21+
22+
val withKotlinModule = jsonMapper {
23+
enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES)
24+
addModule(kotlinModule())
25+
}
26+
27+
assertEquals(expected, withKotlinModule.writeValueAsString(NothingData))
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.core.JsonGenerator
4+
import com.fasterxml.jackson.databind.SerializerProvider
5+
import com.fasterxml.jackson.databind.annotation.JsonSerialize
6+
import com.fasterxml.jackson.databind.ser.std.StdSerializer
7+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
8+
import org.junit.jupiter.api.Assertions.assertEquals
9+
import org.junit.jupiter.api.Test
10+
11+
class GitHub618 {
12+
@JsonSerialize(using = V.Serializer::class)
13+
@JvmInline
14+
value class V(val value: String) {
15+
class Serializer : StdSerializer<V>(V::class.java) {
16+
override fun serialize(p0: V, p1: JsonGenerator, p2: SerializerProvider) {
17+
p1.writeString(p0.toString())
18+
}
19+
}
20+
}
21+
22+
data class D(val v: V?)
23+
24+
@Test
25+
fun test() {
26+
val mapper = jacksonObjectMapper()
27+
// expected: {"v":null}, but NullPointerException thrown
28+
assertEquals("""{"v":null}""", mapper.writeValueAsString(D(null)))
29+
}
30+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude
4+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Assertions.assertNotEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class GitHub625 {
10+
@JvmInline
11+
value class Primitive(val v: Int)
12+
13+
@JvmInline
14+
value class NonNullObject(val v: String)
15+
16+
@JvmInline
17+
value class NullableObject(val v: String?)
18+
19+
@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.NON_NULL)
20+
data class Dto(
21+
val primitive: Primitive? = null,
22+
val nonNullObject: NonNullObject? = null,
23+
val nullableObject: NullableObject? = null
24+
) {
25+
fun getPrimitiveGetter(): Primitive? = null
26+
fun getNonNullObjectGetter(): NonNullObject? = null
27+
fun getNullableObjectGetter(): NullableObject? = null
28+
}
29+
30+
@Test
31+
fun test() {
32+
val mapper = jacksonObjectMapper()
33+
val dto = Dto()
34+
assertEquals("{}", mapper.writeValueAsString(dto))
35+
}
36+
37+
@JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL)
38+
data class FailingDto(
39+
val nullableObject1: NullableObject = NullableObject(null),
40+
val nullableObject2: NullableObject? = NullableObject(null),
41+
val map: Map<Any, Any?> = mapOf("nullableObject" to NullableObject(null),)
42+
) {
43+
fun getNullableObjectGetter1(): NullableObject = NullableObject(null)
44+
fun getNullableObjectGetter2(): NullableObject? = NullableObject(null)
45+
fun getMapGetter(): Map<Any, Any?> = mapOf("nullableObject" to NullableObject(null))
46+
}
47+
48+
@Test
49+
fun failing() {
50+
val writer = jacksonObjectMapper()
51+
val json = writer.writeValueAsString(FailingDto())
52+
53+
assertNotEquals("{}", json)
54+
}
55+
}

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github464.kt

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
22

3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder
34
import com.fasterxml.jackson.core.JsonGenerator
45
import com.fasterxml.jackson.databind.JsonSerializer
56
import com.fasterxml.jackson.databind.ObjectMapper
@@ -42,6 +43,20 @@ class Github464 {
4243
// val xyzzy: T get() = quux
4344
}
4445

46+
@JsonPropertyOrder(
47+
"foo",
48+
"bar",
49+
"baz",
50+
"qux",
51+
"quux",
52+
"corge",
53+
"grault",
54+
"garply",
55+
"waldo",
56+
"fred",
57+
"plugh",
58+
// "xyzzy"
59+
)
4560
class Poko(
4661
val foo: ValueClass,
4762
val bar: ValueClass?,
@@ -150,20 +165,22 @@ class Github464 {
150165
}
151166
}
152167

153-
class SerializerPriorityTest {
154-
@JvmInline
155-
value class ValueBySerializer(val value: Int)
168+
@JvmInline
169+
value class ValueBySerializer(val value: Int)
156170

157-
object Serializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
158-
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
159-
gen.writeString(value.value.toString())
160-
}
171+
object Serializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
172+
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
173+
gen.writeString(value.value.toString())
161174
}
162-
object KeySerializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
163-
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
164-
gen.writeFieldName(value.value.toString())
165-
}
175+
}
176+
object KeySerializer : StdSerializer<ValueBySerializer>(ValueBySerializer::class.java) {
177+
override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) {
178+
gen.writeFieldName(value.value.toString())
166179
}
180+
}
181+
182+
@Nested
183+
inner class SerializerPriorityTest {
167184

168185
private val target = mapOf(ValueBySerializer(1) to ValueBySerializer(2))
169186
private val sm = SimpleModule()
@@ -172,8 +189,7 @@ class Github464 {
172189

173190
@Test
174191
fun simpleTest() {
175-
val om: ObjectMapper = jacksonMapperBuilder()
176-
.addModule(sm).build()
192+
val om: ObjectMapper = jacksonMapperBuilder().addModule(sm).build()
177193

178194
assertEquals("""{"1":"2"}""", om.writeValueAsString(target))
179195
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.jacksonMapperBuilder
5+
import io.github.projectmapk.jackson.module.kogera.testPrettyWriter
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class Github536 {
10+
@JvmInline
11+
value class JsonKeyGetter(val value: Int) {
12+
@get:JsonKey
13+
val jsonKey: String
14+
get() = this.toString()
15+
}
16+
17+
interface IJsonKeyGetter {
18+
@get:JsonKey
19+
val jsonKey: String
20+
get() = this.toString()
21+
}
22+
23+
@JvmInline
24+
value class JsonKeyGetterImplementation(val value: Int) : IJsonKeyGetter
25+
26+
@JvmInline
27+
value class JsonKeyGetterImplementationDisabled(val value: Int) : IJsonKeyGetter {
28+
@get:JsonKey(false)
29+
override val jsonKey: String
30+
get() = super.jsonKey
31+
}
32+
33+
private val writer = jacksonMapperBuilder().build().testPrettyWriter()
34+
35+
@Test
36+
fun test() {
37+
val src = mapOf(
38+
JsonKeyGetter(0) to 0,
39+
JsonKeyGetterImplementation(1) to 1,
40+
JsonKeyGetterImplementationDisabled(2) to 2
41+
)
42+
43+
assertEquals(
44+
"""
45+
{
46+
"JsonKeyGetter(value=0)" : 0,
47+
"JsonKeyGetterImplementation(value=1)" : 1,
48+
"2" : 2
49+
}
50+
""".trimIndent(),
51+
writer.writeValueAsString(src)
52+
)
53+
}
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.projectmapk.jackson.module.kogera.zPorted.test.github
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty
4+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class Github630 {
9+
private val mapper = jacksonObjectMapper()
10+
11+
data class Dto(
12+
// from #570, #603
13+
val FOO: Int = 0,
14+
val bAr: Int = 0,
15+
@JsonProperty("b")
16+
val BAZ: Int = 0,
17+
@JsonProperty("q")
18+
val qUx: Int = 0,
19+
// from #71
20+
internal val quux: Int = 0,
21+
// from #434
22+
val `corge-corge`: Int = 0,
23+
// additional
24+
@get:JvmName("aaa")
25+
val grault: Int = 0
26+
)
27+
28+
@Test
29+
fun test() {
30+
val dto = Dto()
31+
32+
assertEquals(
33+
"""{"FOO":0,"bAr":0,"b":0,"q":0,"quux":0,"corge-corge":0,"grault":0}""",
34+
mapper.writeValueAsString(dto)
35+
)
36+
}
37+
}

0 commit comments

Comments
 (0)