You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
KiwiUrls#prependLeadingSlash throws a StringIndexOutOfBoundsException if it is given an empty string. While an empty string may not be normal, sometimes it makes sense. For example, code that accepts a path representing AWS instance metadata at the well-known base URL http://169.254.169.254/latest/meta-data. This URL returns the top-level metadata, while you can do the following to obtain instance tags:
A utility method might accept a path relative to the base URL, such that you could pass in an empty string to represent the top-level of metadata. So, the method might be defined with a signature like:
publicList<InstanceMetadataEntry> getInstanceMetadata(Stringpath) {
varnormalizedPath = isNull(path) ? "/" : KiwiUrls.prependLeadingSlash(path);
// rest of code to make local HTTP request to http://169.254.169.254/latest/meta-data// plus the normalizedPath starting with a /returninstanceMetadataList;
}
In this code with path is an empty string, we would want KiwiUrls.prependLeadingSlash(path) to return a "/", which would then result in the URL http://169.254.169.254/latest/meta-data/.
What about null input? Currently, a NullPointerException is thrown with the message:
java.lang.NullPointerException: Cannot invoke "String.trim()" because "path" is null
We can extend this by accepting strings that consist only of white space, since it is possible for URLs to contain (encoded) white spaces. However, null should never be considered a valid path in the context of a URL, so I think throwing the NPE is fine, especially now that Java provides a useful message. It probably isn't worth adding an argument check, either, just to throw an IllegalArgumentException, but we could do this later if needed for some reason.
The text was updated successfully, but these errors were encountered:
sleberknight
changed the title
KiwiUrls#prependLeadingSlash should accept empty strings
KiwiUrls#prependLeadingSlash should accept empty or white space-only strings
Mar 5, 2024
KiwiUrls#prependLeadingSlash
throws aStringIndexOutOfBoundsException
if it is given an empty string. While an empty string may not be normal, sometimes it makes sense. For example, code that accepts a path representing AWS instance metadata at the well-known base URLhttp://169.254.169.254/latest/meta-data
. This URL returns the top-level metadata, while you can do the following to obtain instance tags:A utility method might accept a path relative to the base URL, such that you could pass in an empty string to represent the top-level of metadata. So, the method might be defined with a signature like:
In this code with
path
is an empty string, we would wantKiwiUrls.prependLeadingSlash(path)
to return a"/"
, which would then result in the URLhttp://169.254.169.254/latest/meta-data/
.What about null input? Currently, a
NullPointerException
is thrown with the message:We can extend this by accepting strings that consist only of white space, since it is possible for URLs to contain (encoded) white spaces. However,
null
should never be considered a valid path in the context of a URL, so I think throwing the NPE is fine, especially now that Java provides a useful message. It probably isn't worth adding an argument check, either, just to throw anIllegalArgumentException
, but we could do this later if needed for some reason.The text was updated successfully, but these errors were encountered: