Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 25ae07b

Browse files
committed
Fix incorrect behaviour of DefaultCodec
1 parent 6e5d778 commit 25ae07b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
kotlin.code.style=official
2-
tide.version=3.6
2+
tide.version=3.7

src/main/kotlin/io/github/dockyardmc/tide/codec/DefaultCodec.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import io.github.dockyardmc.tide.transcoder.Transcoder
55
class DefaultCodec<T>(val inner: Codec<T>, val default: T) : Codec<T> {
66

77
override fun <D> encode(transcoder: Transcoder<D>, value: T): D {
8-
return inner.encode(transcoder, value ?: default)
8+
if (value == null || value == default) return transcoder.encodeNull()
9+
return inner.encode(transcoder, value)
910
}
1011

1112
override fun <D> decode(transcoder: Transcoder<D>, value: D): T {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.dockyardmc.tide
2+
3+
import io.github.dockyardmc.tide.codec.Codec
4+
import io.github.dockyardmc.tide.codec.StructCodec
5+
import io.github.dockyardmc.tide.transcoder.JsonTranscoder
6+
import org.junit.jupiter.api.Test
7+
8+
class DefaultCodecTest {
9+
10+
data class Player(val username: String, val isGay: Boolean) {
11+
companion object {
12+
val CODEC = StructCodec.of(
13+
"username", Codec.STRING, Player::username,
14+
"is_gay", Codec.BOOLEAN.default(false), Player::isGay,
15+
::Player
16+
)
17+
}
18+
}
19+
20+
@Test
21+
fun testDefault() {
22+
val player = Player("LukynkaCZE", false)
23+
val encoded = Player.CODEC.encode(JsonTranscoder, player)
24+
25+
assert(!encoded.toString().contains("isGay"))
26+
}
27+
28+
}

0 commit comments

Comments
 (0)