Skip to content

Commit a9d0d1d

Browse files
github-actions[bot]Jackeyzhejackeyzhe
authored
[#7516] fix: Ensure that converting a string to a namespace validates all input (#7569)
### What changes were proposed in this pull request? Valid input string for fromString in Namespace.java ### Why are the changes needed? Ensure that converting a string to a namespace validates all input Fix: #7516 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? (Please test your changes, and provide instructions on how to test it: add these tests ``` java @test public void testFromString() { Assertions.assertEquals(Namespace.empty(), Namespace.fromString("")); Assertions.assertEquals(Namespace.of("a", "b"), Namespace.fromString("a.b")); Assertions.assertEquals(Namespace.of("a"), Namespace.fromString("a")); } @test public void testFromStringInvalidArgs() { Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString(null)); Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString(".a")); Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString("a.")); Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString("a..b")); } ``` Co-authored-by: Jackeyzhe <[email protected]> Co-authored-by: jackeyzhe <[email protected]>
1 parent ebd1e15 commit a9d0d1d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

api/src/main/java/org/apache/gravitino/Namespace.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.apache.gravitino;
2020

2121
import com.google.common.base.Joiner;
22+
import com.google.common.base.Preconditions;
2223
import com.google.errorprone.annotations.FormatMethod;
2324
import com.google.errorprone.annotations.FormatString;
2425
import java.util.Arrays;
26+
import org.apache.commons.lang3.StringUtils;
2527
import org.apache.gravitino.exceptions.IllegalNamespaceException;
2628

2729
/**
@@ -72,8 +74,12 @@ public static Namespace of(String... levels) {
7274
* @return A namespace with the given levels
7375
*/
7476
public static Namespace fromString(String namespace) {
75-
// todo: escape the dots in the levels if needed
76-
return new Namespace(namespace.split("\\."));
77+
Preconditions.checkArgument(namespace != null, "Cannot create a namespace with null input");
78+
Preconditions.checkArgument(!namespace.endsWith("."), "Cannot create a namespace end with dot");
79+
if (StringUtils.isBlank(namespace)) {
80+
return empty();
81+
}
82+
return Namespace.of(namespace.split("\\."));
7783
}
7884

7985
private Namespace(String[] levels) {

api/src/test/java/org/apache/gravitino/TestNamespace.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ public void testCreateNamespace() {
4646
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.of("a", null, "c"));
4747
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.of("a", "", "c"));
4848
}
49+
50+
@Test
51+
public void testFromString() {
52+
Assertions.assertEquals(Namespace.empty(), Namespace.fromString(""));
53+
Assertions.assertEquals(Namespace.of("a", "b"), Namespace.fromString("a.b"));
54+
Assertions.assertEquals(Namespace.of("a"), Namespace.fromString("a"));
55+
}
56+
57+
@Test
58+
public void testFromStringInvalidArgs() {
59+
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString(null));
60+
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString(".a"));
61+
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString("a."));
62+
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.fromString("a..b"));
63+
}
4964
}

0 commit comments

Comments
 (0)