Skip to content

Commit 01c4035

Browse files
committed
NullPointerException when partial is empty fix #835
- Added tests and safe-guard
1 parent 1706832 commit 01c4035

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/HbsParserFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.github.jknack.handlebars.internal;
1919

2020
import java.io.IOException;
21+
import java.util.Optional;
2122

2223
import org.antlr.v4.runtime.ANTLRErrorListener;
2324
import org.antlr.v4.runtime.CharStream;
@@ -73,7 +74,8 @@ public Template parse(final TemplateSource source) throws IOException {
7374
final ANTLRErrorListener errorReporter = new HbsErrorReporter(sourceName);
7475

7576
// 1. Lexer
76-
String content = source.content(handlebars.getCharset());
77+
String content = Optional.ofNullable(source.content(handlebars.getCharset()))
78+
.orElse("");
7779
final HbsLexer lexer = newLexer(CharStreams.fromString(content, sourceName), startDelimiter, endDelimiter);
7880
configure(lexer, errorReporter);
7981

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.github.jknack.handlebars.i835;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import org.junit.Test;
10+
11+
import com.github.jknack.handlebars.Handlebars;
12+
import com.github.jknack.handlebars.io.ClassPathTemplateLoader;
13+
import com.github.jknack.handlebars.v4Test;
14+
15+
public class Issue835 extends v4Test {
16+
17+
@Override protected void configure(Handlebars handlebars) {
18+
handlebars.with(
19+
new ClassPathTemplateLoader("/" + getClass().getPackage().getName().replace(".", "/")));
20+
}
21+
22+
@Test
23+
public void shouldIgnoreZeroSizeFileTemplate() throws IOException {
24+
assertEquals(0, Files.size(
25+
Paths.get("src", "test", "resources", "com", "github", "jknack", "handlebars", "i835",
26+
"i835.hbs")));
27+
shouldCompileTo("-{{> i835}}-", $(), "--");
28+
}
29+
30+
@Test
31+
public void shouldIgnoreEmptyTemplate() throws IOException {
32+
shouldCompileTo("", $(), "");
33+
}
34+
35+
}

handlebars/src/test/resources/com/github/jknack/handlebars/i835/i835.hbs

Whitespace-only changes.

0 commit comments

Comments
 (0)