Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes NPE in RequestRouting#canonicalize() #1072

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ public void route(BareRequest bareRequest, BareResponse bareResponse) {
}

private static String canonicalize(String p) {
String result = p;
if (p.charAt(p.length() - 1) == '/') {
result = p.substring(0, p.length() - 1);
}
if (result.isEmpty()) {
String result;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  • Should this not have a unit test to avoid issues going forward?
  • Any reason why not a more fail-fast approach which is easier to read:
    private static String canonicalize(String p) {
        if (p == null || p.isEmpty() || p.equals("/")) {
            return "/";
        }
        int lastCharIndex = p.length() - 1;
        if (p.charAt(lastCharIndex) == '/') {
            return p.substring(0, lastCharIndex);
        }
        return p;
    }

I'm interested to know what the benefit is of indenting more or delaying the return of the method when you already know the result.

if (p == null || p.isEmpty() || p.equals("/")) {
result = "/";
} else {
int lastCharIndex = p.length() - 1;
if (p.charAt(lastCharIndex) == '/') {
result = p.substring(0, lastCharIndex);
} else {
result = p;
}
}
return result;
}
Expand Down