8000 Avoid merging message bundle and attributes into a new map by ahus1 · Pull Request #52991 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Avoid merging message bundle and attributes into a new map - #52991

Draft
ahus1 wants to merge 3 commits into
keycloak:mainfrom
ahus1:is-52990-optimize-message-resources
Draft

ahus1 wants to merge 3 commits into
keycloak:mainfrom
ahus1:is-52990-optimize-message-resources

Conversation

@ahus1
@ahus1 ahus1 commented Sep 20, 2026 •
Copy link
Copy Markdown
Member

FreeMarkerLoginFormsProvider used to merge the message bundle and template attributes into a new HashMap on every page render, even though the result is only used for read-only lookups.

Replaced the merge with MessageAttributeProperties, a read-only Properties view that checks the message bundle first and falls back to the template attributes, without copying either source.

This is a deliberate, not just incidental, behavior change: attribute lookups now reflect the attributes map as of render time (via a live reference) instead of a snapshot taken when the view was constructed.
FreeMarkerLoginFormsProvider.processTemplate() resolves native FreeMarker ${...} expressions against the same, fully-populated attributes map only after createCommonAttributes() finishes running.
A ${...} placeholder embedded inside a translated message string now resolves consistently with the same placeholder written directly in a template file, closing a previous inconsistency where only the message-string path saw an earlier, incomplete snapshot.

Closes #52990

FreeMarkerLoginFormsProvider used to merge the message bundle and template
attributes into a new HashMap on every page render, even though the
result is only used for read-only lookups.

Replaced the merge with MessageAttributeProperties, a read-only
Properties view that checks the message bundle first and falls back
to the template attributes, without copying either source.

Closes keycloak#52990

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Top-level message lookups currently bypass the new view’s lookup logic and return message keys instead of translations.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

Introduces a zero-copy message and template-attribute lookup view for login page rendering.

Changes:

  • Adds MessageAttributeProperties.
  • Uses it in the FreeMarker login provider.
  • Adds lookup and immutability tests.
File Description
MessageAttributeProperties.java Implements the combined lookup view.
FreeMarkerLoginFormsProvider.java Replaces per-render map copying.
MessageAttributePropertiesTest.java Tests precedence, live attributes, and mutations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The live attribute view contradicts the issue’s requirement to preserve the previous snapshot behavior.

Review effort: Balanced
Findings: None

Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Preserve attribute snapshot semantics for message formatting

services/​src/​main/​java/​org/​keycloak/​theme/​beans/​MessageAttributeProperties.java:43

This live reference changes the behavior that #52990 requires preserving: the previous new HashMap<>(attributes) (followed by the MessageFormatterMethod(Map) copy) snapshotted entries before createCommonAttributes() and the attribute mapper ran, whereas this view exposes later additions and replacements. That can change ${...} substitutions in custom messages; please retain the original snapshot semantics while removing the full merge, or explicitly revise the requirement if this behavior change is intended.

@ahus1
ahus1 commented Sep 20, 2026
Copy link
Copy Markdown
Member Author

Re: the "Previously missed" finding about preserving attribute snapshot semantics (MessageAttributeProperties.java:43).

This is a deliberate behavior change, not an oversight. FreeMarkerLoginFormsProvider.processTemplate() resolves native FreeMarker ${...} expressions directly against attributes, but only after createCommonAttributes() has finished populating it. The old snapshot (taken much earlier, in handleThemeResources()) meant a ${...} placeholder embedded inside a translated message string could see a different, incomplete view of attributes than the exact same placeholder written directly in a template file. The live reference removes that inconsistency: both paths now resolve attributes as of render time.

Updated #52990's Goals section and this PR's description to state this explicitly, and reworded the Javadoc/test in the latest commit to document the reasoning instead of only describing the mechanism.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The implementation preserves lookup precedence, avoids copying, and includes focused coverage of the new behavior.

Review effort: Balanced
Findings: None

@ahus1
ahus1 force-pushed the is-52990-optimize-message-resources branch from 5fbd1dd to 5d1b3b5 Compare September 20, 2026 17:12
@ahus1
ahus1 requested a balanced review from Copilot September 20, 2026 17:12
Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Replacement maps returned by attributeMapper are not reflected in translated-message placeholder resolution.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
@ahus1
ahus1 force-pushed the is-52990-optimize-message-resources branch from 5d1b3b5 to e79cbc9 Compare September 20, 2026 17:17
@ahus1
ahus1 requested a balanced review from Copilot September 20, 2026 17:23
Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Attribute mappers can replace the final render map, leaving message interpolation bound to a different attribute map.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Nested placeholders do not resolve consistently with native FreeMarker expressions as documented.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@stianst stianst added this to the 27.1 milestone Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid merging the message bundle and template attributes into a new map on every page render

3 participants

0