Skip to content

Commit 9e933f0

Browse files
committed
fix(key): Ensure keys with invalid namespaces throw the correct exception
Also improve nullability checks on key methods.
1 parent a4bc4d6 commit 9e933f0

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

key/src/main/java/net/kyori/adventure/key/Key.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package net.kyori.adventure.key;
2525

2626
import java.util.Comparator;
27+
import java.util.Objects;
2728
import java.util.OptionalInt;
2829
import java.util.stream.Stream;
2930
import net.kyori.examination.Examinable;
@@ -105,6 +106,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
105106
*/
106107
@SuppressWarnings("PatternValidation") // impossible to validate since the character is variable
107108
static @NotNull Key key(final @NotNull String string, final char character) {
109+
Objects.requireNonNull(string, "string");
108110
final int index = string.indexOf(character);
109111
final String namespace = index >= 1 ? string.substring(0, index) : MINECRAFT_NAMESPACE;
110112
final String value = index >= 0 ? string.substring(index + 1) : string;
@@ -121,7 +123,7 @@ public interface Key extends Comparable<Key>, Examinable, Namespaced, Keyed {
121123
* @since 4.4.0
122124
*/
123125
static @NotNull Key key(final @NotNull Namespaced namespaced, @KeyPattern.Value final @NotNull String value) {
124-
return key(namespaced.namespace(), value);
126+
return key(Objects.requireNonNull(namespaced, "namespaced").namespace(), value);
125127
}
126128

127129
/**
@@ -185,6 +187,7 @@ static boolean parseableNamespace(final @NotNull String namespace) {
185187
* @since 4.14.0
186188
*/
187189
static @NotNull OptionalInt checkNamespace(final @NotNull String namespace) {
190+
Objects.requireNonNull(namespace, "namespace");
188191
for (int i = 0, length = namespace.length(); i < length; i++) {
189192
if (!allowedInNamespace(namespace.charAt(i))) {
190193
return OptionalInt.of(i);
@@ -212,6 +215,7 @@ static boolean parseableValue(final @NotNull String value) {
212215
* @since 4.14.0
213216
*/
214217
static @NotNull OptionalInt checkValue(final @NotNull String value) {
218+
Objects.requireNonNull(value, "value");
215219
for (int i = 0, length = value.length(); i < length; i++) {
216220
if (!allowedInValue(value.charAt(i))) {
217221
return OptionalInt.of(i);

key/src/main/java/net/kyori/adventure/key/KeyImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ final class KeyImpl implements Key {
4545
private final String value;
4646

4747
KeyImpl(final @NotNull String namespace, final @NotNull String value) {
48-
checkError("namespace", namespace, value, Key.checkNamespace(namespace));
49-
checkError("value", namespace, value, Key.checkValue(value));
48+
checkError("namespace", namespace, namespace, value, Key.checkNamespace(namespace), NAMESPACE_PATTERN);
49+
checkError("value", value, namespace, value, Key.checkValue(value), VALUE_PATTERN);
5050
this.namespace = requireNonNull(namespace, "namespace");
5151
this.value = requireNonNull(value, "value");
5252
}
5353

54-
private static void checkError(final String name, final String namespace, final String value, final OptionalInt index) {
54+
private static void checkError(final String name, final String checkPart, final String namespace, final String value, final OptionalInt index, final String pattern) {
5555
if (index.isPresent()) {
5656
final int indexValue = index.getAsInt();
57-
final char character = value.charAt(indexValue);
57+
final char character = checkPart.charAt(indexValue);
5858
throw new InvalidKeyException(namespace, value, String.format(
59-
"Non [a-z0-9_.-] character in %s of Key[%s] at index %d ('%s', bytes: %s)",
59+
"Non " + pattern + " character in %s of Key[%s] at index %d ('%s', bytes: %s)",
6060
name,
6161
asString(namespace, value),
6262
indexValue,

key/src/test/java/net/kyori/adventure/key/KeyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,9 @@ void testNulChar() {
124124
void testParseWithEmptyNamespace() {
125125
assertEquals(Key.key(Key.MINECRAFT_NAMESPACE, "test"), Key.key(":test"));
126126
}
127+
128+
@Test
129+
void testKeyWithInvalidCharacter() {
130+
assertThrows(InvalidKeyException.class, () -> Key.key("a/b", "a"));
131+
}
127132
}

0 commit comments

Comments
 (0)