Skip to content

fix: write the calendar labels as valid JSON - #433

Open
ChristianMurphy wants to merge 1 commit into
uPortal-Project:masterfrom
ChristianMurphy:fix/calendar-labels-json
Open

ChristianMurphy wants to merge 1 commit into
uPortal-Project:masterfrom
ChristianMurphy:fix/calendar-labels-json

Conversation

@ChristianMurphy

Copy link
Copy Markdown
Member
Checklist
Description of change

Problem. Since 2.7.4, a user whose language is French gets the Calendar with English labels, and the browser console shows calendar-portlet: invalid i18n JSON SyntaxError: Bad escaped character in JSON. calendarNarrowView.jsp and calendarWideView.jsp pass the labels to <calendar-portlet> as JSON in a <script type="application/json">, and write each label with <spring:message javaScriptEscape='true'/>. Spring's JavaScript escaping writes an apostrophe as \', and JSON has no such escape. messages_fr.properties has view.more.events=Voir plus d'évènements, so JSON.parse fails in the narrow view and the element falls back to its English labels. The wide view has the same pattern. It works today only because none of its French labels has an apostrophe.

Fix. A new tag, <upcal:jsonMessage code="..."/>, takes the place of spring:message in both label blocks. JsonMessageTag extends Spring's MessageTag, so the message is found the same way as before, and the tag writes it escaped for a JSON string with Jackson's JsonStringEncoder. It also escapes < and >, as the old escaping did, so a label can't end the script element. The TLD is WEB-INF/tag/calendar.tld, next to uportal.tld, and include.jsp declares the prefix. The tag needs the JSP API to compile. The parent pom already manages tomcat-jsp-api as a provided dependency. The portlet's pom now names it, without a version. No other JSP uses javaScriptEscape.

Test. JsonMessageTagTest renders view.more.events from the portlet's own French bundle through the tag and puts the result in a JSON string. Jackson parses that string, and the test expects Voir plus d'évènements. A second test escapes a text with an apostrophe, quotes, a backslash, </script>, a tab, a line break and a vertical tab, and checks that it reads back unchanged and has no < or > left. When I ran the first test with spring:message and javaScriptEscape in place of the new tag, it failed with JsonParseException: Unrecognized character escape ''' (code 39).

Verified. mvn -B package, the command CI runs, passes on this branch on JDK 11 with 33 tests. I also checked the change in a portal, in GitHub Actions: uPortal-start's master as it is (uPortal 5.17.9, HSQLDB, Tomcat 8.5, JDK 11). A browser signed in as student, chose French in the language portlet, and opened the Calendar on the Welcome tab (the narrow view) and maximized (the wide view). With the released 2.7.4, the narrow view showed Day, Week, Month and "View more events", and the console had the error above. The wide view was French. Then the job deployed this branch's war in the same portal. Both views showed Jour, Semaine and Mois, the narrow view's link read "Voir plus d'évènements", and the element didn't log an error. So Tomcat finds the new TLD and the tag works in both JSPs. The workflow and scripts for this run are on my uPortal-start fork's branch test/verify-portlet-fixes.

Not verified. The portal check used Chromium and Tomcat 8.5 only. French is the only translation the portlet has, so no other language was checked.

Where this comes from. I'm working on a fork that upgrades uPortal's dependencies (Spring 5.3 and Hibernate 5.6 so far, heading for Spring 7, Hibernate 7 and Jakarta EE 11). That fork's CI runs uPortal-start's Playwright suite against HSQLDB, PostgreSQL, MySQL, MariaDB, SQL Server and Oracle. A test there that expects an empty console failed on the Welcome page while its user's language was French. The bug isn't caused by the upgrade: the portlet in that portal was the released 2.7.4, and the two JSPs are the same in CalendarPortlet-2.7.4 and master, so I prepared the fix here on its own. Until a release has it, my uPortal-start fork overlays the two JSPs with copies that repair the JSON.

The two calendar views pass their labels to <calendar-portlet> as JSON
in a script element, each written with spring:message and
javaScriptEscape. That escaping writes an apostrophe as \', which JSON
doesn't allow. The French bundle has "Voir plus d'évènements", so for
a French user JSON.parse failed, the element logged "invalid i18n
JSON" and showed its English labels.

A new tag, upcal:jsonMessage, resolves the message as spring:message
does and escapes it for a JSON string. It also escapes < and >, as the
old escaping did, so a label can't end the script element. Both views
use it for every label. The tag needs the JSP API to compile, which
the parent pom already manages as a provided dependency.
@Naenyn
Naenyn self-requested a review November 14, 2026 03:10

@Naenyn Naenyn 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.

Thanks, this looks good to me. Approving.

The failed macOS build isn't from your change. It's the same thing as on NewsReaderPortlet#450: this branch still has the duplicate maven-antrun-plugin declaration, and CI ran a bare mvn, so the macOS runner's Maven 3.10 failed while the 3.9 runners passed. master has since fixed the duplicate (#435) and pins Maven 3.10.0 through the wrapper (#429). The branch merges cleanly with current master. On that merge, ./mvnw -B package passes on Java 11 with all 33 tests, and license:check passes. Merging master into the branch should turn CI green.

The fix.

  • Extending MessageTag and overriding only writeMessage keeps message resolution identical, and JsonStringEncoder plus the \u003C/\u003E replacement covers both JSON validity and ending the <script> element.
  • Exposing only code in the TLD means nobody can turn javaScriptEscape back on for this tag.
  • tomcat-jsp-api stays out of the WAR (provided scope via the parent). The built WAR has JsonMessageTag.class and WEB-INF/tag/calendar.tld.

One small thing to be aware of, not a change request: MessageTag applies HTML escaping before writeMessage, so if defaultHtmlEscape were ever enabled for the portlet, labels would get HTML entities inside the JSON. Nothing enables it today.

I didn't run it in a portal myself; the above is from building and testing locally on Java 11.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants