fix: case-sensitive key lookup for IDictionary/Hashtable (issue #521)#638
Merged
Conversation
The DictionaryMemberAccessor was using ChainSegment.LowerInvariant to
index into a non-generic IDictionary (e.g. Hashtable), silently
downcasing the lookup key. This meant uppercase keys like "NAME" could
never be found via {{NAME}}, while lowercase keys resolved incorrectly
for any casing in the template.
The bug was introduced in commit 9d52004 when the string memberName
parameter was replaced with ChainSegment: LowerInvariant was chosen
instead of TrimmedValue. The fix restores the original exact-match
behaviour, which matches Handlebars.js (JS object keys are always
case-sensitive).
Fixes #521
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Hashtable(and any other non-genericIDictionary) with an uppercase key (e.g."NAME") was silently returning empty when accessed via{{NAME}}in a template, whileDictionary<string, string>with the same key worked correctly.Hashtablewith a lowercase key also incorrectly resolved for any casing in the template expression, violating case-sensitive JS object key semantics.Root cause
In
DictionaryMemberAccessor.TryGetValue, the lookup was performed usingmemberName.LowerInvariantinstead ofmemberName.TrimmedValue. This bug was introduced in commit9d52004when thestring memberNameparameter was replaced withChainSegment memberName—LowerInvariantwas chosen instead ofTrimmedValue.The original code (pre-refactor) used the raw string with no case folding, which is the correct behaviour.
Fix
One-line change in
source/Handlebars/MemberAccessors/DictionaryMemberAccessor.cs:JS parity
Handlebars.js resolves object keys case-sensitively (
{{NAME}}on{ NAME: "bob" }works;{{NAME}}on{ name: "bob" }does not). This fix restores that behaviour forIDictionary/Hashtable.Test plan
Four new regression tests added to
IssueTests.cs:Hashtablewith uppercase key resolves correctly via matching expressionHashtablewith lowercase key still resolves via matching expressionDictionary<string, string>with uppercase key still works (no regression){{name}}does NOT resolve aHashtablekey"NAME"(case-sensitive, JS-parity)All 1750 existing tests pass.
🤖 Generated with Claude Code