Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(serializer-gson): Ignore empty hover event values #859

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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