snazy commented on code in PR #10877: URL: https://github.com/apache/iceberg/pull/10877#discussion_r1711255861
########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -194,15 +192,34 @@ public static String decodeString(String encoded) { * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter */ public static String encodeNamespace(Namespace ns) { - Preconditions.checkArgument(ns != null, "Invalid namespace: null"); - String[] levels = ns.levels(); + return encodeNamespace(ns, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Returns a String representation of a namespace that is suitable for use in a URL / URI. + * + * <p>This function needs to be called when a namespace is used as a path variable (or query + * parameter etc.), to format the namespace per the spec. Review Comment: Superfluous `,` ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -194,15 +192,34 @@ public static String decodeString(String encoded) { * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter */ public static String encodeNamespace(Namespace ns) { - Preconditions.checkArgument(ns != null, "Invalid namespace: null"); - String[] levels = ns.levels(); + return encodeNamespace(ns, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Returns a String representation of a namespace that is suitable for use in a URL / URI. + * + * <p>This function needs to be called when a namespace is used as a path variable (or query + * parameter etc.), to format the namespace per the spec. + * + * <p>{@link RESTUtil#decodeNamespace(String, String)} should be used to parse the namespace from + * a URL parameter. + * + * @param namespace namespace to encode + * @param separator The namespace separator to use for encoding + * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter Review Comment: This isn't true, if the namespace contains elements with characters mentioned [here](https://jakarta.ee/specifications/servlet/6.0/jakarta-servlet-spec-6.0#uri-path-canonicalization). This either deserves a comment or validation. ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -215,8 +232,32 @@ public static String encodeNamespace(Namespace ns) { * @return a namespace */ public static Namespace decodeNamespace(String encodedNs) { - Preconditions.checkArgument(encodedNs != null, "Invalid namespace: null"); - String[] levels = Iterables.toArray(NAMESPACE_ESCAPED_SPLITTER.split(encodedNs), String.class); + return decodeNamespace(encodedNs, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Takes in a string representation of a namespace as used for a URL parameter and returns the + * corresponding namespace. + * + * <p>See also {@link #encodeNamespace} for generating correctly formatted URLs. + * + * @param encodedNamespace a namespace to decode + * @param separator The namespace separator to use for decoding. This should be the same separator Review Comment: should? not "must"? ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -215,8 +232,32 @@ public static String encodeNamespace(Namespace ns) { * @return a namespace */ public static Namespace decodeNamespace(String encodedNs) { - Preconditions.checkArgument(encodedNs != null, "Invalid namespace: null"); - String[] levels = Iterables.toArray(NAMESPACE_ESCAPED_SPLITTER.split(encodedNs), String.class); + return decodeNamespace(encodedNs, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Takes in a string representation of a namespace as used for a URL parameter and returns the Review Comment: Same here - what's a "URL parameter"? ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -194,15 +192,34 @@ public static String decodeString(String encoded) { * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter */ public static String encodeNamespace(Namespace ns) { - Preconditions.checkArgument(ns != null, "Invalid namespace: null"); - String[] levels = ns.levels(); + return encodeNamespace(ns, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Returns a String representation of a namespace that is suitable for use in a URL / URI. + * + * <p>This function needs to be called when a namespace is used as a path variable (or query + * parameter etc.), to format the namespace per the spec. + * + * <p>{@link RESTUtil#decodeNamespace(String, String)} should be used to parse the namespace from + * a URL parameter. + * + * @param namespace namespace to encode + * @param separator The namespace separator to use for encoding + * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter Review Comment: What's a "URL parameter"? A URI path element? A URI query part? ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -215,8 +232,32 @@ public static String encodeNamespace(Namespace ns) { * @return a namespace */ public static Namespace decodeNamespace(String encodedNs) { - Preconditions.checkArgument(encodedNs != null, "Invalid namespace: null"); - String[] levels = Iterables.toArray(NAMESPACE_ESCAPED_SPLITTER.split(encodedNs), String.class); + return decodeNamespace(encodedNs, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Takes in a string representation of a namespace as used for a URL parameter and returns the + * corresponding namespace. + * + * <p>See also {@link #encodeNamespace} for generating correctly formatted URLs. + * + * @param encodedNamespace a namespace to decode + * @param separator The namespace separator to use for decoding. This should be the same separator Review Comment: Is this URL-encoded or not the URL-encoded? Allowing both is dangerous, right? ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -215,8 +232,32 @@ public static String encodeNamespace(Namespace ns) { * @return a namespace */ public static Namespace decodeNamespace(String encodedNs) { Review Comment: This function looks dangerous now. ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -215,8 +232,32 @@ public static String encodeNamespace(Namespace ns) { * @return a namespace */ public static Namespace decodeNamespace(String encodedNs) { - Preconditions.checkArgument(encodedNs != null, "Invalid namespace: null"); - String[] levels = Iterables.toArray(NAMESPACE_ESCAPED_SPLITTER.split(encodedNs), String.class); + return decodeNamespace(encodedNs, NAMESPACE_ESCAPED_SEPARATOR); + } + + /** + * Takes in a string representation of a namespace as used for a URL parameter and returns the + * corresponding namespace. + * + * <p>See also {@link #encodeNamespace} for generating correctly formatted URLs. + * + * @param encodedNamespace a namespace to decode + * @param separator The namespace separator to use for decoding. This should be the same separator + * that was used when calling {@link RESTUtil#encodeNamespace(Namespace, String)} + * @return a namespace + */ + public static Namespace decodeNamespace(String encodedNamespace, String separator) { + Preconditions.checkArgument(encodedNamespace != null, "Invalid namespace: null"); + Preconditions.checkArgument( + !Strings.isNullOrEmpty(separator), "Invalid separator: null or empty"); + + // use legacy splitter for backwards compatibility in case an old clients encoded the namespace + // with %1F + Splitter splitter = + encodedNamespace.contains(NAMESPACE_ESCAPED_SEPARATOR) Review Comment: How can this trigger if `separator` is not `\u001f"? ########## core/src/main/java/org/apache/iceberg/rest/RESTUtil.java: ########## @@ -194,15 +192,34 @@ public static String decodeString(String encoded) { * @return UTF-8 encoded string representing the namespace, suitable for use as a URL parameter */ public static String encodeNamespace(Namespace ns) { Review Comment: This function looks dangerous now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org