Skip to content

Commit

Permalink
Merge pull request #859 from KyoriPowered/fix/414
Browse files Browse the repository at this point in the history
fix(serializer-gson): Ignore empty hover event values
  • Loading branch information
kashike committed Feb 13, 2023
2 parents 12bbd34 + 1438b88 commit 1d2631b
Show file tree
Hide file tree
Showing 2 changed files with 22 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 (isNullOrEmpty(rawValue)) {
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 (isNullOrEmpty(element)) {
value = null;
} else {
final Component rawValue = this.gson.fromJson(element, SerializerFactory.COMPONENT_TYPE);
value = this.legacyHoverEventContents(action, rawValue);
}
} else {
value = null;
}
Expand All @@ -177,6 +184,10 @@ public Style read(final JsonReader in) throws IOException {
return style.build();
}

private static boolean isNullOrEmpty(final @Nullable JsonElement element) {
return element == null || element.isJsonNull() || (element.isJsonArray() && element.getAsJsonArray().size() == 0) || (element.isJsonObject() && element.getAsJsonObject().size() == 0);
}

private boolean readBoolean(final JsonReader in) throws IOException {
final JsonToken peek = in.peek();
if (peek == JsonToken.BOOLEAN) {
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 1d2631b

Please sign in to comment.