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

Fix NPE in BasicRestProjectGenerator when a resource is missing #3480

Merged
merged 1 commit into from
Aug 12, 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 @@ -5,7 +5,9 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -111,9 +113,15 @@ private void generate(final String templateName, final Map<String, Object> conte
throws IOException {
if (!writer.exists(outputFilePath)) {
String path = templateName.startsWith("/") ? templateName : "/" + templateName;
try (final BufferedReader stream = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(path), StandardCharsets.UTF_8))) {
String template = stream.lines().collect(Collectors.joining("\n"));
URL resource = getClass().getResource(path);
if (resource == null) {
throw new IOException("Template resource is missing: " + path);
}
try (
InputStream resourceStream = resource.openStream();
InputStreamReader streamReader = new InputStreamReader(resourceStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader)) {
String template = bufferedReader.lines().collect(Collectors.joining("\n"));
for (Entry<String, Object> e : context.entrySet()) {
if (e.getValue() != null) { // Exclude null values (classname and path can be null)
template = template.replace(format("${%s}", e.getKey()), e.getValue().toString());
Expand Down