From 49b8f5330ad8bf0b7203d507c356028014170b98 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 6 May 2022 19:22:46 +0100 Subject: Fix NPE in config provider delete method (#199) --- .../serverconfig/AbstractServerConfigProvider.java | 25 ++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'spark-common/src/main/java/me') diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java index d09eb7c..56b3731 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java @@ -27,9 +27,11 @@ import com.google.gson.JsonObject; import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Deque; import java.util.LinkedList; import java.util.Map; +import java.util.stream.Collectors; /** * Abstract implementation of {@link ServerConfigProvider}. @@ -99,13 +101,28 @@ public abstract class AbstractServerConfigProvider> implements } JsonObject jsonObject = json.getAsJsonObject(); - String member = path.removeFirst().replace("", "."); + String expected = path.removeFirst().replace("", "."); - if (member.equals("*") || jsonObject.has(member)) { + Collection keys; + if (expected.equals("*")) { + keys = jsonObject.entrySet().stream() + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } else if (jsonObject.has(expected)) { + keys = Collections.singletonList(expected); + } else { + keys = Collections.emptyList(); + } + + for (String key : keys) { if (path.isEmpty()) { - jsonObject.remove(member); + jsonObject.remove(key); } else { - delete(jsonObject.get(member), path); + Deque pathCopy = expected.equals("*") + ? new LinkedList<>(path) + : path; + + delete(jsonObject.get(key), pathCopy); } } } -- cgit