Skip to content

Let a partial page update deliver named data to client-side code - #1907

Open
papegaaij wants to merge 6 commits into
masterfrom
partial-update-data
Open

papegaaij wants to merge 6 commits into
masterfrom
partial-update-data

Conversation

@papegaaij

Copy link
Copy Markdown
Contributor

An Ajax response can now carry named data for client-side code, besides components, header contributions and scripts. On the server, addData(name, value) on IPartialPageRequestHandler (and so on AjaxRequestTarget and the web socket request handler) adds an entry. It is written as a <data name="…"> element in the <ajax-response>, and both Ajax libraries publish it on the new topic '/ajax/call/data'.

Until now, client-side code that needed a value from the server got it through an evaluated script, typically appendJavaScript("x.update(" + json + ")") with an escaping helper. That mixes data with code, has to escape correctly for a script context, and has to pass the Trusted Types policy of #1880. A data entry is never interpreted or evaluated by Wicket. It is published in order, after the components have been replaced and the scripts evaluated, and before the success handlers run. It also works for updates pushed over a web socket.

This is split out of #1834 (Wicket Islands), which uses it as the result channel of its server calls, following the review there. It stands on its own, and other libraries can use it in place of appendJavaScript.

Changes

  • IPartialPageRequestHandler#addData(String, CharSequence), implemented in AbstractPartialPageRequestHandler.
  • PartialPageUpdate#addData and the abstract writeData, implemented in XmlPartialPageUpdate as <data name="…"><![CDATA[…]]></data>.
  • wicket-ajax-jquery.js and wicket-ajax.js publish '/ajax/call/data' with (attrs, name, value). Over a web socket, attrs is an empty object.
  • Reference guide: the topic is listed with the other global Ajax events.
  • migration-to-12.md: a section on the two new abstract methods.

Compatibility

addData and writeData are new abstract methods. Applications using AjaxRequestTarget, the web socket request handler or XmlPartialPageUpdate see no change. A class that implements IPartialPageRequestHandler without extending AbstractPartialPageRequestHandler, or that extends PartialPageUpdate without extending XmlPartialPageUpdate, must implement the new method. writeData stays abstract: PartialPageUpdate does not know the response format, so a default could only drop the data silently.

Tests

XmlPartialPageUpdateTest and AjaxRequestHandlerTest cover the server side, and the QUnit tests in wicket-core/src/test/js/ajax.js cover both Ajax libraries. mvn clean verify -Pjs-test passes.

🤖 Generated with Claude Code

papegaaij and others added 3 commits November 5, 2026 18:57
An Ajax response could carry components, header contributions and
scripts, but no plain data. Client-side code that needed a value from the
server had to receive it through an evaluated script, for example by
appending JavaScript that stores JSON somewhere for later pickup. That
mixes data with code, depends on where the script happens to run, and
offers no ordering guarantee relative to the rest of the response.

IPartialPageRequestHandler (and with it AjaxRequestTarget and the
websocket request handler) now has addData(name, value). Each entry is
written as a <data name="..."> element in the <ajax-response>. Both Ajax
libraries publish it on the new topic '/ajax/call/data' with the call's
attributes, the name and the value, after the components have been
replaced and the scripts evaluated, and before the success handlers run.
Wicket delivers the value as text and never interprets it.

This adds an abstract method to IPartialPageRequestHandler, implemented
in AbstractPartialPageRequestHandler, and an abstract writeData to
PartialPageUpdate, implemented in XmlPartialPageUpdate. Applications that
use these classes see no change. A custom implementation that does not
extend those base classes has to implement the new method. The data part
is the foundation for embedding JavaScript framework components, which
need a result channel for server calls that is separate from scripts.

Migration guide: "IPartialPageRequestHandler#addData(String,
CharSequence) and PartialPageUpdate#writeData(Response, String,
CharSequence) are new abstract methods. Implementations that do not
extend AbstractPartialPageRequestHandler or XmlPartialPageUpdate must
implement them."

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the migration section

The review of the data part in pull request #1834 asked for two points
in the Javadoc of IPartialPageRequestHandler#addData. A partial update
pushed over a web socket belongs to no Ajax call, so the subscribers of
'/ajax/call/data' get an empty object as attrs; the Javadoc only spoke
of the attributes of the Ajax call. And every subscriber sees every
entry, with the name as the only thing telling them apart, so two
libraries choosing the same name would read each other's data; the
Javadoc now recommends a name prefixed with the library's name.

The data part added two abstract methods, which breaks implementations
that do not extend the base classes, so migration-to-12.md gets the
section the commit introducing them only drafted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 63.97%. Comparing base (1220fb6) to head (123a529).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1907      +/-   ##
============================================
+ Coverage     63.95%   63.97%   +0.01%     
- Complexity    12265    12276      +11     
============================================
  Files          1276     1276              
  Lines         50398    50431      +33     
  Branches       7127     7132       +5     
============================================
+ Hits          32232    32261      +29     
- Misses        15402    15404       +2     
- Partials       2764     2766       +2     
🚀 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.

@reiern70 reiern70 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, Emond. I read it and ran it merged onto current master: the tests of wicket-core, wicket-core-tests and wicket-native-websocket-core, and the QUnit suites for both engines, pass. It stands on its own well outside Islands. addData is a cleaner way to hand values to client-side code than appendJavaScript plus an escaping helper.

What I checked and agree with:

  • The data steps run after the header contributions, components and evaluations of the same response. A library that a header item in the same response loads is therefore ready when its data arrives, and the order doesn't depend on where <data> sits in the XML.
  • A ]]> in a value is split across CDATA sections, and Wicket.DOM.text joins them again. Your QUnit response covers that.
  • Data can be added until the components are written (from renderHead or onBeforeRespond), and isEmpty() counts it, so a web socket push that carries only data is still sent.
  • Abstract methods rather than defaults that silently drop the data: agreed, and fine for 12.

Points:

  1. "Exactly as given" isn't quite true, and some values break the whole response. The value travels in XML, so the browser's XML parser applies two rules to it:

    • Line breaks are normalized: \r\n and a lone \r arrive as \n.
    • Characters that XML 1.0 does not allow make the response unparsable: U+0000 to U+0008, U+000B, U+000C, U+000E to U+001F, U+FFFE, U+FFFF and unpaired surrogates.

    I checked both in Chromium with DOMParser on a <data> element like the one writeData writes. "a\r\nb" came back as "a\nb". "a\u000Bb" gave a parser error; a vertical tab is common in text pasted from Word. On a parser error the whole call fails with "Error while parsing response", so none of the components in that response are replaced either.

    Components and scripts have the same limits today, but a data value is much more likely to be raw user text. JSON is safe, because encoders escape control characters. I'd suggest:

    • addData rejects a value with characters XML cannot carry, with an IllegalArgumentException that names the entry. The error then shows up on the server, where it was made, rather than as a parse error in the browser.
    • The Javadoc of IPartialPageRequestHandler#addData replaces "exactly as given": line breaks arrive as \n, and arbitrary text should be sent in an encoding such as JSON.
  2. Conflicts with master. wicket-ajax.js, wicket-ajax-jquery.js and ajax_6.adoc conflict with the queued and dropped topics from #1997; keep both sides. In migration-to-12.md, master no longer has the PageParameters.hashCode() section that your hunk sits under, so the data section goes in as a section of its own. I resolved the conflicts locally for the test run, and the diff against master stayed the same 308 lines.

  3. Reference guide (nit). The '/ajax/call/data' entry mentions only AjaxRequestTarget. As the Javadoc does, it could also mention web socket pushes, where attrs is an empty object.

With 1 and 2 addressed, this looks good to me.

papegaaij and others added 2 commits December 2, 2026 09:30
The topic constants and the guide's list of topics keep both the data
topic and the queued and dropped topics from #1997. Master no longer has
the PageParameters.hashCode() section the migration section sat under, so
the data section is now a section of its own at the end of the guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A data entry travels in a CDATA section of the XML Ajax response, so the
browser's XML parser applies its rules to it. Line breaks are normalized,
\r\n and a lone \r arriving as \n, and characters that XML 1.0 does not
allow, such as a vertical tab in text pasted from a word processor, make
the whole response unparsable. The call then failed in the browser with
"Error while parsing response", and none of the components in that
response were replaced either. The Javadoc nevertheless promised the value
would arrive exactly as given.

addData now rejects a name or value containing such a character with an
IllegalArgumentException naming the entry, so the error shows up on the
server, where it was made. The Javadoc and the reference guide say that
line breaks arrive as \n and that arbitrary text should be sent in an
encoding such as JSON, whose encoders escape these characters. The guide
entry also mentions web socket pushes, whose subscribers receive an empty
attrs object.

For components, Wicket's answer to the same characters remains the opt-in
XmlCleaningResponseFilter, which strips them. A data value that silently
loses characters is worse than an error, so data entries are rejected
instead, and the filter never finds anything to strip in them. The
character test moves to Strings.isValidXmlChar, which the filter now uses
as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@papegaaij

Copy link
Copy Markdown
Contributor Author

Thanks, Ernesto, for the thorough check.

  1. Values XML can't carry: done as you suggested, in 4fbbbf0.
    • addData now rejects a name or value containing a character that XML 1.0 doesn't allow. It throws an IllegalArgumentException that names the entry, the character and its index.
    • The Javadoc of IPartialPageRequestHandler#addData no longer says "exactly as given". It says that line breaks arrive as \n, and that arbitrary text should go in an encoding such as JSON.
    • The character test moved to Strings.isValidXmlChar, which XmlCleaningResponseFilter now uses too.
    • For components, the opt-in filter still strips these characters. For data I preferred an error, because a value that silently loses characters is worse.
  2. Conflicts: master is merged in fb0f279.
    • Both sides of the topics are kept: data, queued and dropped.
    • The migration entry is now a section of its own at the end of migration-to-12.md.
    • The diff against master was the same 308 lines before the fix for point 1.
  3. Guide: the '/ajax/call/data' entry now mentions web socket pushes, where attrs is an empty object. It also has the line-break and JSON wording.

A full mvn clean verify -Pjs-test is green.

papegaaij added a commit that referenced this pull request Dec 2, 2026
Brings in the review fixes of #1907, rejecting data entries that XML
cannot carry, and current master. The poms keep the modules and the
managed dependencies of both sides in sorted order: wicket-dbsc from
master next to wicket-islands, and the examples depend on wicket-dbsc and
wicket-resumable-upload as well. Selenium takes master's newer version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@reiern70 reiern70 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, Emond. All three points are addressed, and rejecting on the server is the right call.

One nit on wording I suggested myself: JSON encoders do not escape all of these characters. Jackson 3.3 escapes the control characters, but writes U+FFFE, U+FFFF and unpaired surrogates as they are unless JsonWriteFeature.ESCAPE_NON_ASCII is on. The Javadoc and the guide could say that such an encoder has to escape these too.

This has a consequence in Islands, see my comment on #1834.

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

All three points are addressed, thanks Emond. The nit on the wording about JSON encoders from my last comment is not a blocker.

The Javadoc of addData and the reference guide said that JSON encoders
escape the characters XML cannot carry. They escape the control
characters, but not all of them escape the noncharacters U+FFFE and
U+FFFF or unpaired surrogates: Jackson writes those as they are unless it
is told to escape non-ASCII characters. Such a value is then rejected by
addData. Both now list these characters and say the encoder has to
escape them too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@papegaaij

Copy link
Copy Markdown
Contributor Author

Thanks, Ernesto. The wording is fixed in 123a529. The Javadoc of addData and the guide now list U+FFFE, U+FFFF and unpaired surrogates next to the control characters, and say the encoder has to escape these too, since not every JSON encoder does.

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.

3 participants