Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine
MIT License
1.24k stars 214 forks source link

Unable to retrieve dictionary values in nested call to the each helper when the data is a mix of Dictionary and List. #466

Open cjvirtucio87 opened 3 years ago

cjvirtucio87 commented 3 years ago

Describe the bug

When using data that is a mix of Dictionary and List instances and a template string that uses an #each helper nested inside another #each helper call, the template is unable to retrieve dictionary values inside the nested #each.

Expected behavior:

A template string that has a retrieval attempt of a dictionary value within a nested #each call results in that value being written to the final output.

Test to reproduce

(this is taken from this unit test I've written on a fork):


        [Theory, ClassData(typeof(HandlebarsEnvGenerator))]
        public void NestedEachDictionariesLists(IHandlebars handlebars)
        {
            var source =
                @"
                {{#each this.users}}
                    {{#each friends}}
                        {{emailAddress}}
                    {{/each}}
                {{/each}}
                ";
            var template = handlebars.Compile(source);

            var friends = new List<Dictionary<object, object>>
            {
                new Dictionary<object, object>
                {
                    { "emailAddress", "foobarbaz@baz.com" }
                },
            };
            var users = new List<Dictionary<object, object>>
            {
                new Dictionary<object, object>
                { 
                    { "friends",  friends },
                },
            };
            var data = new Dictionary<object, object>
            {
                { "users",  users },
            };

            Assert.Equal(
                "foobarbaz@baz.com",
                template(data).Trim());
        }

Other related info

The above test fails with the following output:

  Error Message:
   Assert.Equal() Failure
          ↓ (pos 0)
Expected: foobarbaz@baz.com
Actual:
          ↑ (pos 0)
  Stack Trace:
     at HandlebarsDotNet.Test.BasicIntegrationTests.NestedEachStronglyTyped(IHandlebars handlebars) in /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars.Test/BasicIntegrationTests.cs:line 423
cjvirtucio87 commented 3 years ago

Seems to work fine when using arrays:

$ dotnet test Handlebars.Test/Handlebars.Test.csproj --filter 'FullyQualifiedName~HandlebarsDotNet.Test.BasicIntegrationTests.NestedEachDictionariesArrays'

  Determining projects to restore...
  All projects are up-to-date for restore.
  Handlebars -> /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars/bin/Debug/netstandard2.0/Handlebars.dll
  Handlebars -> /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars/bin/Debug/netstandard2.1/Handlebars.dll
  Handlebars.Test -> /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars.Test/bin/Debug/netcoreapp2.1/Handlebars.Test.dll
  Handlebars.Test -> /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars.Test/bin/Debug/netcoreapp3.1/Handlebars.Test.dll
Test run for /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars.Test/bin/Debug/netcoreapp2.1/Handlebars.Test.dll (.NETCoreApp,Version=v2.1)
Microsoft (R) Test Execution Command Line Tool Version 16.10.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 17 ms - /mnt/c/Users/cvirtucio/git/cjvirtucio87-Handlebars.Net/source/Handlebars.Test/bin/Debug/netcoreapp2.1/Handlebars.Test.dll (netcoreapp2.1)