Skip to content

fix: drop cached descriptor when the context value changes type - #686

Closed
bardrop wants to merge 1 commit into
Handlebars-Net:masterfrom
bardrop-oss:fix-descriptor-reset-on-value-type-change
Closed

bardrop wants to merge 1 commit into
Handlebars-Net:masterfrom
bardrop-oss:fix-descriptor-reset-on-value-type-change

Conversation

@bardrop

@bardrop bardrop commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #685.

Iterators reuse one BindingContext and only assign innerContext.Value for each item. BindingContext.Descriptor is a lazy value built from the first item's type and was only reset when the context went back to the pool. So in {{#each items}}{{name}}{{/each}}:

  • dictionary first, then a number/string/list → InvalidCastException
  • number first, then a dictionary → empty output

The Value setter now resets Descriptor when the new value has a different type. Same-type items keep the cached descriptor, so the common path does no extra work (ObjectDescriptor.Create only depends on the type).

Tests: IteratorWithItemsOfDifferentTypes (array) and ObjectIteratorWithValuesOfDifferentTypes (dictionary values) in IteratorTests.cs fail before the change and pass after. Full suite: 2236 passed. On 30k random templates compared with handlebars.js 4.7.8, the 70 InvalidCastException failures are gone and no output that matched before changed.

Iterators reuse one BindingContext and only assign Value for each item.
The member accessor cached in Descriptor belonged to the first item's
type, so a later item of another type threw InvalidCastException (or a
later dictionary rendered empty). Reset the descriptor when the value
type changes.

Fixes Handlebars-Net#685
@bardrop

bardrop commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Closing since #691 landed the same fix on master. Thanks for picking it up.

@bardrop bardrop closed this Nov 22, 2026
@stencil-harness stencil-harness Bot mentioned this pull request Nov 22, 2026
jsneedles pushed a commit to jsneedles/Handlebars.Net that referenced this pull request Nov 25, 2026
Iterators reuse one BindingContext and only assign Value for each item.
The member accessor cached in Descriptor belonged to the first item's
type, so a later item of another type threw InvalidCastException (or a
later dictionary rendered empty). Reset the descriptor when the value
type changes; same-type items keep the cached descriptor.

Based on Handlebars-Net#686 by @bardrop.

Fixes Handlebars-Net#685

Co-Authored-By: Mradul Singh <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

#each: member lookup throws InvalidCastException when items have different types

1 participant