Skip to content

Support escaping a GSP expression with a backslash \${} - #16144

Merged
jdaugherty merged 1 commit into
apache:8.0.xfrom
codeconsole:feature/gsp-escaped-dollar-8.0.x
Aug 13, 2026
Merged

jdaugherty merged 1 commit into
apache:8.0.xfrom
codeconsole:feature/gsp-escaped-dollar-8.0.x

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Summary

A backslash immediately before a GSP expression now escapes it: \${...} renders a literal ${...} with the backslash removed, instead of the expression being evaluated on the server.

GSP previously had no escape syntax at all — \${x} rendered a stray \ followed by the evaluated expression. The motivating case is JavaScript template literals, whose interpolation placeholders share the ${} syntax and are today either evaluated against the page binding (an unbound name silently renders as empty) or blow up the whole page render:

<script>
    const name = 'Grails';
    console.log(`Hello \${name}`);
</script>

The browser now receives the template literal with its ${name} placeholder intact, to be interpolated by JavaScript.

Implementation

Escaping is resolved entirely at parse time, in two small pieces:

  • GroovyPageScanner no longer starts an expression token when the ${ is immediately preceded by a backslash in template text — the sequence stays in the surrounding HTML token.
  • GroovyPageParser.html() rewrites \${ to ${ when writing the HTML token out.

Because this happens before code generation, behavior is identical in development, production, and compileStatic modes, and escaped text is invisible to static type checking — an escaped ${undeclaredName} under <%@ page compileStatic="true" %> neither fails compilation nor evaluates (covered by a new test in GspCompileStaticSpec).

Semantics

  • Only the backslash performing the escape is removed: \\${x} renders \${x}.
  • Each expression is escaped individually.
  • Expressions in the attributes of GSP tags are scanned in a different lexer state and cannot be escaped this way.

Also in this PR

grails-gsp-core declared junit-jupiter-api but never the Jupiter engine, so the JUnit Platform silently skipped every Jupiter-annotated test class in the module — all of GroovyPagesTemplateEngineTests (23 tests) never ran. Added the repo-standard testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'; all resurrected tests pass. The other engine-less modules with Jupiter tests (grails-gsp/plugin, grails-gsp/grails-taglib) receive the engine transitively and were already running them.

Tests and docs

  • ScanTests: escaped ${ stays in the HTML token; unescaped expressions still tokenize; mixed escaped/unescaped side by side.
  • GroovyPagesTemplateEngineTests: literal render, escaped next to evaluated, JavaScript template literal end-to-end, double-backslash semantics.
  • GspCompileStaticSpec: escaped expressions are invisible to static compilation.
  • Guide: escaping documented in the GSP Basics "Expressions" section with the JavaScript template literal example.

Full :grails-gsp-core:test and :grails-gsp:test suites plus downstream :grails-taglib, :grails-sitemesh3, :grails-layout, and :grails-web-gsp all pass; codeStyle is clean on the touched modules.

https://claude.ai/code/session_01WtCxMe9pfmy7tiHCUr5THN

\${...} in template text now renders a literal ${...} instead of being
evaluated: the scanner leaves the escaped sequence in the surrounding
HTML token and the parser drops the backslash when writing it out.
Escaping is resolved at parse time, before code generation, so all
compilation modes behave identically and escaped text is invisible to
static compilation. The primary use is JavaScript template literals,
whose interpolation placeholders share the ${...} syntax.

Only the backslash performing the escape is removed, each expression
must be escaped individually, and expressions in tag attributes cannot
be escaped this way.

Also adds the junit-jupiter-engine test runtime dependency that
grails-gsp-core was missing: the module declared only the Jupiter API,
so the JUnit Platform silently skipped its Jupiter test classes,
including all of GroovyPagesTemplateEngineTests.

Claude-Session: https://claude.ai/code/session_01WtCxMe9pfmy7tiHCUr5THN
@codecov

codecov Bot commented Aug 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 52.6414%. Comparing base (fa1e147) to head (daaa404).

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16144        +/-   ##
==================================================
+ Coverage     52.5503%   52.6414%   +0.0911%     
- Complexity      18391      18436        +45     
==================================================
  Files            2037       2037                
  Lines           96498      96500         +2     
  Branches        16860      16860                
==================================================
+ Hits            50710      50799        +89     
+ Misses          38353      38272        -81     
+ Partials         7435       7429         -6     
Files with missing lines Coverage Δ
...oovy/org/grails/gsp/compiler/GroovyPageParser.java 73.8526% <100.0000%> (+3.2398%) ⬆️
...ovy/org/grails/gsp/compiler/GroovyPageScanner.java 88.7324% <100.0000%> (+2.9168%) ⬆️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codeconsole
codeconsole requested review from borinquenkid, jamesfredley, jdaugherty, matrei and sbglasius and removed request for jdaugherty August 13, 2026 01:42
@codeconsole codeconsole changed the title Support escaping a GSP expression with a backslash Support escaping a GSP expression with a backslash JS Template \${var} Aug 13, 2026
@codeconsole codeconsole changed the title Support escaping a GSP expression with a backslash JS Template \${var} Support escaping a GSP expression with a backslash \${} Aug 13, 2026
@codeconsole codeconsole added this to the grails:8.0.0-RC1 milestone Aug 13, 2026
@testlens-app

testlens-app Bot commented Aug 13, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: daaa404
▶️ Tests: 67957 executed
⚪️ Checks: 77/77 completed


Learn more about TestLens at testlens.app.

@matrei matrei left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a note in the upgrade section as it could break someones gsp output?

@jdaugherty
jdaugherty merged commit 4431ece into apache:8.0.x Aug 13, 2026
80 checks passed
@jdaugherty

Copy link
Copy Markdown
Contributor

@codeconsole I had merged this before seeing @matrei comment. can you please update the upgrade section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants