Skip to content

Commit

Permalink
bug(#792): don't throw on invalid click/hover event action
Browse files Browse the repository at this point in the history
fixes #792
  • Loading branch information
kashike committed Jun 29, 2022
1 parent 167e46d commit 1c2463f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
Expand Up @@ -27,7 +27,7 @@
import net.kyori.adventure.text.event.ClickEvent;

final class ClickEventActionSerializer {
static final TypeAdapter<ClickEvent.Action> INSTANCE = IndexedSerializer.of("click action", ClickEvent.Action.NAMES);
static final TypeAdapter<ClickEvent.Action> INSTANCE = IndexedSerializer.lenient("click action", ClickEvent.Action.NAMES);

private ClickEventActionSerializer() {
}
Expand Down
Expand Up @@ -27,7 +27,7 @@
import net.kyori.adventure.text.event.HoverEvent;

final class HoverEventActionSerializer {
static final TypeAdapter<HoverEvent.Action<?>> INSTANCE = IndexedSerializer.of("hover action", HoverEvent.Action.NAMES);
static final TypeAdapter<HoverEvent.Action<?>> INSTANCE = IndexedSerializer.lenient("hover action", HoverEvent.Action.NAMES);

private HoverEventActionSerializer() {
}
Expand Down
Expand Up @@ -33,14 +33,20 @@
final class IndexedSerializer<E> extends TypeAdapter<E> {
private final String name;
private final Index<String, E> map;
private final boolean throwOnUnknownKey;

public static <E> TypeAdapter<E> of(final String name, final Index<String, E> map) {
return new IndexedSerializer<>(name, map).nullSafe();
public static <E> TypeAdapter<E> strict(final String name, final Index<String, E> map) {
return new IndexedSerializer<>(name, map, true).nullSafe();
}

private IndexedSerializer(final String name, final Index<String, E> map) {
public static <E> TypeAdapter<E> lenient(final String name, final Index<String, E> map) {
return new IndexedSerializer<>(name, map, false).nullSafe();
}

private IndexedSerializer(final String name, final Index<String, E> map, final boolean throwOnUnknownKey) {
this.name = name;
this.map = map;
this.throwOnUnknownKey = throwOnUnknownKey;
}

@Override
Expand All @@ -54,8 +60,10 @@ public E read(final JsonReader in) throws IOException {
final E value = this.map.value(string);
if (value != null) {
return value;
} else {
} else if (this.throwOnUnknownKey) {
throw new JsonParseException("invalid " + this.name + ": " + string);
} else {
return null;
}
}
}
Expand Up @@ -27,7 +27,7 @@
import net.kyori.adventure.text.format.TextDecoration;

final class TextDecorationSerializer {
static final TypeAdapter<TextDecoration> INSTANCE = IndexedSerializer.of("text decoration", TextDecoration.NAMES);
static final TypeAdapter<TextDecoration> INSTANCE = IndexedSerializer.strict("text decoration", TextDecoration.NAMES);

private TextDecorationSerializer() {
}
Expand Down
Expand Up @@ -28,6 +28,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

// https://github.com/KyoriPowered/adventure/issues/788
class Issue788Test {
@Test
void test() {
Expand Down
@@ -0,0 +1,39 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.serializer.gson;

import net.kyori.adventure.text.Component;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

// https://github.com/KyoriPowered/adventure/issues/792
class Issue792Test {
@Test
void test() {
final String input = "[\"\",{\"text\":\"\",\"extra\":[{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{marriagemaster_heart}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\" \",\"color\":\"white\"},{\"text\":\"{vault_prefix}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{player_displayname}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\"\",\"color\":\"white\"},{\"text\":\"{vault_suffix}\",\"color\":\"white\",\"clickEvent\":{\"action\":\"\",\"value\":\"\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"\"}]}}},{\"text\":\" \",\"color\":\"white\"},{\"text\":\"»\",\"color\":\"gray\"}]},{\"text\":\"\",\"color\":\"white\"},{\"text\":\" f\",\"color\":\"white\"},{\"text\":\" [✓]\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/vchatgui .DoctorMad9952 Global 421783436\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Open Moderation GUI\",\"color\":\"red\"}]}}}]";
final Component component = GsonComponentSerializer.gson().deserialize(input);
assertNotNull(component);
}
}

0 comments on commit 1c2463f

Please sign in to comment.