Skip to content

Commit

Permalink
#211 - NPE in RouterParser class in case a parameter is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHossfeld committed Jul 29, 2021
1 parent ff8f0c8 commit 037cd06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
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//"));
}

}

0 comments on commit 037cd06

Please sign in to comment.