Skip to content

Custom handlebars helper results in ClassCastException on options.fn(this) #58

@jgremmen

Description

@jgremmen

I followed the documentation on adding additional HandleBars, like this:

tasks.named('gitChangelog', se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
  templateContent.set(layout.projectDirectory.dir("gradle").file("changelog.mustache").asFile.getText("UTF-8"))

  handlebarsHelpers.set([
      new HelperParam("ifCommitFeature", (String message, Options options) -> {
        return message.startsWith('feat: ')
            ? options.fn(this)
            : options.inverse(this)
      }),
  ])

When I use {{#ifCommitFeature messageTitle}} inside {{#commits}} I get the following exception from Gradle:

GitChangelog
com.github.jknack.handlebars.HandlebarsException: inline@79e75c35:23:9: java.lang.ClassCastException: class changelog_485vat4mka7v2dyta2ln8v3vq cannot be cast to class se.bjurr.gitchangelog.api.model.Commit (changelog_485vat4mka7v2dyta2ln8v3vq is in unnamed module of loader org.gradle.groovy.scripts.internal.DefaultScriptCompilationHandler$ScriptClassLoader @5923efd9; se.bjurr.gitchangelog.api.model.Commit is in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader @fa8fde4)
    inline@79e75c35:23:9
	at com.github.jknack.handlebars.internal.Block.merge(Block.java:199)
	at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:97)
	at com.github.jknack.handlebars.internal.TemplateList.merge(TemplateList.java:81)
	at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:97)
	at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:86)
	at com.github.jknack.handlebars.Options.apply(Options.java:511)
	at com.github.jknack.handlebars.Options.fn(Options.java:417)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)

Initially the helper had Commit as its 1st parameter. Based on the exception I changed the commit to a regular string. Now I can at least see it enter my helper but on options.fn(this) it throws the exception.

I know the current code I posted doesn't justify its own helper but it is a starting point for a more complex evaluation.

Is there a way to solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions