handlebars.java
handlebars.java copied to clipboard
TemplateSource's need better, stricter equals()
Most TemplateSource
implementations inherit equals()
from AbstractTemplateSource
which only compares filename()
.
This means that two equally named TemplateSource
's, even loaded from different media (ClassPath, filesystem, Internet, whatever), will be considered identical by the template cache system, which can cause caching false positives. So probably URLTemplateSource
should be considered equal only to another URLTemplateSource
with the same location
(not name
!).
StringTemplateSource
is a place where this problem can surface even without using different template loaders and fancy stuff like that. It gets its filename
from the constructor parameter; in Handlebars.compileInline()
, the name passed to the constructor is derived from the template string's hashCode()
. In case of hash code collision, two different template strings will have the same filename
in StringTemplateSource
and will thus be considered equal by the cache system, which will yield the same Template
for two different template strings. A totally unexpected, rare, and thus hard to catch failure. StringTemplateSource
must compare content
, not filename
!
Got hit by this issues recently! Currently the equals()
only compares the filename.
should definitely compare content if the filename is the same for StringTemplateSource
. It's easily to hit collision when having a lot strings.