Skip to content

Commit 559d337

Browse files
kezzTurboJax
authored andcommitted
fix(gson): Better handle null input
This still isn't perfect, but we shouldn't break the contract and return `null` when we parse any other primitive string as a text component.
1 parent 8fc9785 commit 559d337

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

text-serializer-commons/src/main/java/net/kyori/adventure/text/serializer/commons/ComponentTreeConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public final class ComponentTreeConstants {
9393
@ApiStatus.Obsolete
9494
public static final String SHOW_ITEM_TAG = "tag";
9595
public static final String SHOW_ITEM_COMPONENTS = "components";
96+
public static final String NULL = "null";
9697

9798
private ComponentTreeConstants() {
9899
throw new IllegalStateException("Cannot instantiate");

text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.jspecify.annotations.Nullable;
3737

3838
import static java.util.Objects.requireNonNull;
39+
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.NULL;
3940

4041
final class GsonComponentSerializerImpl implements GsonComponentSerializer {
4142
private static final Optional<Provider> SERVICE = Services.service(Provider.class);
@@ -85,6 +86,7 @@ public UnaryOperator<GsonBuilder> populator() {
8586

8687
@Override
8788
public Component deserialize(final String string) {
89+
if (NULL.equals(string)) return Component.text(NULL);
8890
return this.serializer().fromJson(string, Component.class);
8991
}
9092

text-serializer-json/src/testFixtures/java/net/kyori/adventure/text/serializer/json/JSONComponentSerializerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@
2929
import net.kyori.adventure.text.serializer.commons.ComponentTreeConstants;
3030
import org.junit.jupiter.api.Test;
3131

32+
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.NULL;
3233
import static org.junit.jupiter.api.Assertions.assertEquals;
33-
import static org.junit.jupiter.api.Assertions.assertNull;
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
3435
import static org.junit.jupiter.api.Assertions.assertThrows;
3536

3637
final class JSONComponentSerializerTest extends SerializerTest {
3738
@Test
3839
void testDeserializeNull() {
39-
assertNull(JSONComponentSerializer.json().deserialize("null"));
40+
assertNotNull(JSONComponentSerializer.json().deserialize(NULL));
41+
assertEquals(Component.text(NULL), JSONComponentSerializer.json().deserialize(NULL));
4042
}
4143

4244
@Test

0 commit comments

Comments
 (0)