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

#211 - NPE in RouterParser class in case a parameter is not set #213

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
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 @@ -9,22 +9,22 @@
import java.util.stream.Stream;

public class RouteParser {

private static RouteParser instance = new RouteParser();

private SimpleEventBus eventBus;

private RouteParser() {
}

public static RouteParser get() {
return instance;
}

public void setEventBus(SimpleEventBus eventBus) {
this.eventBus = eventBus;
}

/**
* Parse the hash and divides it into shellCreator, route and parameters
*
Expand Down Expand Up @@ -147,7 +147,7 @@ RouteResult parse(String route,
}
return routeResult;
}

/**
* Generates a new route!
* <p>
Expand All @@ -166,7 +166,7 @@ String generate(String route,
routeValue = routeValue.substring(1);
}
String[] partsOfRoute = routeValue.split("/");

int parameterIndex = 0;
for (String s : partsOfRoute) {
sb.append("/");
Expand All @@ -175,21 +175,29 @@ String generate(String route,
sb.append(":");
}
if (params.length - 1 >= parameterIndex) {
sb.append(params[parameterIndex].replace("/",
RouterConstants.NALU_SLASH_REPLACEMENT));
if (!Objects.isNull(params[parameterIndex])) {
sb.append(params[parameterIndex].replace("/",
RouterConstants.NALU_SLASH_REPLACEMENT));
}
parameterIndex++;
}
} else {
sb.append(s);
}
}

// in case there are more parameters then placesholders, we add them add the end!
long numberOfPlaceHolders = Stream.of(partsOfRoute)
.filter(s -> "*".equals(s) || s.startsWith(":"))
.count();
if (params.length > numberOfPlaceHolders) {
String sbExeption = "Warning: route >>" + route + "<< has less parameter placeholder >>" + numberOfPlaceHolders + "<< than the number of parameters in the list of parameters >>" + params.length + "<< --> adding Parameters add the end of the url";
String sbExeption = "Warning: route >>" +
route +
"<< has less parameter placeholder >>" +
numberOfPlaceHolders +
"<< than the number of parameters in the list of parameters >>" +
params.length +
"<< --> adding Parameters add the end of the url";
this.eventBus.fireEvent(LogEvent.create()
.sdmOnly(true)
.addMessage(sbExeption));
Expand All @@ -207,7 +215,7 @@ String generate(String route,
parameterIndex++;
}
}

// remove leading '/'
String generatedRoute = sb.toString();
if (generatedRoute.startsWith("/")) {
Expand All @@ -222,5 +230,5 @@ String generate(String route,
}
return generatedRoute;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,14 @@ void generate04() {
is("application/person/list/A"));
}

@Test
void generate05() {
String hash = RouteParser.get()
.generate("/application/person/list/:name/:citty",
null,
null);
MatcherAssert.assertThat(hash,
is("application/person/list//"));
}

}