Permalink
Browse files

Fixed NPE if model is empty but globals are defined.

  • Loading branch information...
codedance committed Feb 27, 2012
1 parent db42114 commit 7b6b35435c10b5d4e0ad3022ad2df3b5f752e57a
View
@@ -4,7 +4,7 @@
<groupId>com.papercut.silken</groupId>
<artifactId>silken</artifactId>
<packaging>jar</packaging>
- <version>2012-02-23</version>
+ <version>2012-02-27-SNAPSHOT</version>
<name>silken</name>
<url>https://github.com/codedance/silken</url>
<description>Silken - a nicer tasting Soy Tofu (Google Closure Templates)</description>
@@ -282,10 +282,12 @@ private void doRequest(HttpServletRequest req, HttpServletResponse resp) throws
SoyMapData model = config.getModelResolver().resolveModel(req);
SoyMapData globals = null;
if (config.getRuntimeGlobalsProvider() != null) {
-
// Merge globals into the model
globals = config.getRuntimeGlobalsProvider().getGlobals(req);
- model = Utils.mergeSoyMapData(model, globals);
+ if (model != null && globals != null) {
+ // If keys in global clash with model, model wins.
+ model = Utils.mergeSoyMapData(globals, model);
+ }
}
// FUTURE: A mime type resolver and character type encoding?
@@ -294,7 +296,6 @@ private void doRequest(HttpServletRequest req, HttpServletResponse resp) throws
resp.getWriter().print(templateRenderer.render(templateName, model, globals, locale));
return;
-
} catch (Exception e) {
error(req, resp, e);
}
@@ -129,12 +129,15 @@ public static Object toSoyCompatibleObjects(Object obj) {
}
/**
- * Merge two SoyMapData resources.
+ * Merge two SoyMapData resources. If the two maps to merge contain duplicate key names, source
+ * s2 will overwrite s1.
* @param s1 1st resource map.
* @param s2 2nd resource map.
* @return A new SoyMapData object containing data from both source.
*/
public static SoyMapData mergeSoyMapData(SoyMapData s1, SoyMapData s2) {
+ Preconditions.checkNotNull(s1);
+ Preconditions.checkNotNull(s2);
SoyMapData merged = new SoyMapData();
for (String key: s1.getKeys()) {
merged.putSingle(key, s1.getSingle(key));

0 comments on commit 7b6b354

Please sign in to comment.