spring-projects / spring-projects/spring-framework
WebFlux RequestContext should support responseEncodedHtmlEscape like Spring MVC
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 60.2k
- Forks
- 38.8k
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 27
Description
Background
Spring MVC's RequestContext has had two HTML escape flags since Spring 4.1.2 (see SPR-12350 / #16955):
defaultHtmlEscape— the master switch; controls whether escaping happens at allresponseEncodedHtmlEscape— controls whichHtmlUtils.htmlEscape()overload is used when escaping is active
PR #36400 (7.0.6) wired defaultHtmlEscape into WebFlux: WebHttpHandlerBuilder → HttpWebHandlerAdapter → ServerWebExchange.HTML_ESCAPE_ATTRIBUTE → WebFlux RequestContext. That closes half the story. responseEncodedHtmlEscape was not included and remains completely absent from WebFlux.
What responseEncodedHtmlEscape does
When defaultHtmlEscape=true, the two flags combine as follows (from HtmlEscapingAwareTag):
if (isHtmlEscape()) {
if (isResponseEncodedHtmlEscape()) {
out = HtmlUtils.htmlEscape(content, response.getCharacterEncoding()); // encoding-aware
} else {
out = HtmlUtils.htmlEscape(content); // ISO-8859-1, escapes everything
}
}
The difference matters in practice. Given the string "café" with a UTF-8 response:
| Configuration | Output |
|---|---|
defaultHtmlEscape=false |
café |
defaultHtmlEscape=true, responseEncodedHtmlEscape=false |
café <b> |
defaultHtmlEscape=true, responseEncodedHtmlEscape=true |
café <b> |
With a UTF-8 response, the encoding-aware path only escapes the 5 XML-significant characters and lets native UTF-8 characters pass through unchanged. The one-arg overload converts them to HTML entities unnecessarily. MVC's default for responseEncodedHtmlEscape falls back to true, so MVC apps automatically get the correct behavior. WebFlux apps with defaultHtmlEscape=true currently always take the ISO-8859-1 path because the second flag does not exist.
Proposed solution
Mirror the exact pattern from PR #36400 across the same four files:
-
ServerWebExchange— add constantRESPONSE_ENCODED_HTML_ESCAPE_ATTRIBUTE -
WebHttpHandlerBuilder— add field and builder methodresponseEncodedHtmlEscape -
HttpWebHandlerAdapter— add field, setter, and stamp onto exchange increateExchange() -
WebFlux
RequestContext— add field, read from exchange in constructor, exposeisResponseEncodedHtmlEscape()andgetResponseEncodedHtmlEscape(), and use an encoding-aware escape helper
The one difference from MVC is how the response charset is resolved. MVC calls response.getCharacterEncoding(). In WebFlux the equivalent is exchange.getResponse().getHeaders().getContentType().getCharset() with a UTF-8 fallback, which is consistent with how charset is resolved elsewhere in the framework. The default for isResponseEncodedHtmlEscape() falls back to true when unset, matching MVC exactly. No breaking change.
I implemented the defaultHtmlEscape wiring in PR #36400 and I'm prepared to open a PR and implement this follow-up across the same files
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the four WebFlux files named in the issue with the wiring pattern from PR #36400: ServerWebExchange, WebHttpHandlerBuilder, HttpWebHandlerAdapter, and WebFlux RequestContext. Done means the new flag flows through the exchange, exposes both accessors, uses the response charset with UTF-8 fallback, and preserves MVC's true default behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100