Skip to content

fix(runtime): keep class instances intact when partial is invoked with a hash - #2201

Open
cdytotoro-ctrl wants to merge 1 commit into
handlebars-lang:masterfrom
cdytotoro-ctrl:fix/issue-2043-class-instance-partial-context
Open

cdytotoro-ctrl wants to merge 1 commit into
handlebars-lang:masterfrom
cdytotoro-ctrl:fix/issue-2043-class-instance-partial-context

Conversation

@cdytotoro-ctrl

Copy link
Copy Markdown

Invoking a partial with a hash flattened the context via Utils.extend({}, context, options.hash), replacing class instances with plain objects. Helpers inside the partial then received a context without prototype methods or getters, breaking templates rendered over class-based models (Fixes #2043).

The merged context now keeps the original prototype for class instances (objects whose prototype is neither Object.prototype nor null and which are not arrays or maps), so methods and getters still work through 'this' while hash values continue to override context properties. Plain objects, arrays, maps and primitives keep the previous flat-copy behavior, and prototype access control gating is unchanged.

Tests: new cases in spec/partials.js verify helper method calls and instanceof checks inside hashed partials, plus hash override semantics for class instances.

…h a hash

Invoking a partial with a hash flattened the context with
Utils.extend({}, context, options.hash), replacing class instances with
plain objects. Helpers inside the partial then received a context
without any prototype methods or getters, breaking templates rendered
over class-based models (Fixes handlebars-lang#2043).

The merged context now keeps the original prototype for class instances
(any object whose prototype is neither Object.prototype nor null and
which is not an array or a map), so methods and getters still work
through 'this' while hash values continue to override context
properties. Plain objects, arrays, maps and primitives keep the
previous flat-copy behavior.

Fixes handlebars-lang#2043

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.

🟡 Changes recommended

The context-copying change causes hashed partials to throw for Set and Date contexts and prevents reliable hash overrides of inherited accessors or non-writable properties.

2 open issues
What changed in this PR

This PR addresses #2043 by preserving class-instance prototypes when merging hash arguments into partial contexts, allowing helpers to access model methods.

Changes:

  • Adds prototype-preserving context merging for class instances while retaining flat-copy behavior for plain objects, arrays, maps, and primitives.
  • Adds tests for helper method calls, instanceof checks, and hash overrides.
File Description
spec/​partials.js Adds class-instance and hash-override regression tests.
lib/​handlebars/​runtime.js Introduces prototype-preserving merging for partial hash arguments.

Review effort: Balanced

Comment thread lib/handlebars/runtime.js
typeof context === 'object' &&
context !== null &&
!Utils.isArray(context) &&
!Utils.isMap(context)
Comment thread lib/handlebars/runtime.js
Comment on lines +31 to +32
Utils.extend(instance, context);
Utils.extend(instance, hash);

This branch has not been deployed

No deployments
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.

Handlebars does not support class instances in the template data

2 participants