Skip to content

Commit

Permalink
fix(serializer-gson): Ignore empty hover event values
Browse files Browse the repository at this point in the history
Fixes GH-414
  • Loading branch information
zml2008 committed Feb 12, 2023
1 parent 42ea3df commit 2c654f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public Style read(final JsonReader in) throws IOException {
if (hoverEventObject.has(HOVER_EVENT_CONTENTS)) {
final @Nullable JsonElement rawValue = hoverEventObject.get(HOVER_EVENT_CONTENTS);
final Class<?> actionType = action.type();
if (SerializerFactory.COMPONENT_TYPE.isAssignableFrom(actionType)) {
if (rawValue.isJsonNull() || (rawValue.isJsonArray() && rawValue.getAsJsonArray().size() == 0) || (rawValue.isJsonObject() && rawValue.getAsJsonObject().size() == 0)) {
value = null;
} else if (SerializerFactory.COMPONENT_TYPE.isAssignableFrom(actionType)) {
value = this.gson.fromJson(rawValue, SerializerFactory.COMPONENT_TYPE);
} else if (SerializerFactory.SHOW_ITEM_TYPE.isAssignableFrom(actionType)) {
value = this.gson.fromJson(rawValue, SerializerFactory.SHOW_ITEM_TYPE);
Expand All @@ -157,8 +159,13 @@ public Style read(final JsonReader in) throws IOException {
value = null;
}
} else if (hoverEventObject.has(HOVER_EVENT_VALUE)) {
final Component rawValue = this.gson.fromJson(hoverEventObject.get(HOVER_EVENT_VALUE), SerializerFactory.COMPONENT_TYPE);
value = this.legacyHoverEventContents(action, rawValue);
final JsonElement element = hoverEventObject.get(HOVER_EVENT_VALUE);
if (element.isJsonNull() || (element.isJsonArray() && element.getAsJsonArray().size() == 0) || (element.isJsonObject() && element.getAsJsonObject().size() == 0)) {
value = null;
} else {
final Component rawValue = this.gson.fromJson(element, SerializerFactory.COMPONENT_TYPE);
value = this.legacyHoverEventContents(action, rawValue);
}
} else {
value = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ void testDeserializeJsonNull() {
assertEquals("Don't know how to turn null into a Component", ex.getMessage());
}

// https://github.com/KyoriPowered/adventure/issues/414
@Test
void testSkipInvalidHoverEvent() {
final String input = "{\"text\": \"hello\", \"hoverEvent\":{\"action\":\"show_text\",\"value\":[]}}";
final Component expected = Component.text("hello");
assertEquals(expected, GsonComponentSerializer.gson().deserialize(input));
}

private static String name(final NamedTextColor color) {
return NamedTextColor.NAMES.key(color);
}
Expand Down

0 comments on commit 2c654f4

Please sign in to comment.