Handlebars-Net/Handlebars.Net

Exception thrown for immutable array

Closed this issue · 2 comments

Describe the bug

Calling HandlebarsTemplate fails when context contains data of type ImmutableArray<T>.

Expected behavior:

Immutable arrays (structs in general) are supported as valid data types in context for template.

Test to reproduce

[Fact]
public void ImmutableArrayTest()
{
    var itemsList = new List<string> { "hello", "world" };
    var items = ImmutableArray.CreateRange(itemsList);

    string source =
        @"{{#each Items}}
            {{this}}
          {{/each}}";

    var template = Handlebars.Compile(source);
    var result = template(new { Items = items }); // This throws exception
}

Other related info

Immutable arrays were supported in version 1.1.15. After upgrading to 2.0.7 calling the HandlebarsTemplate throws System.ArgumentException with message: "Method HandlebarsDotNet.ObjectDescriptors.EnumerableObjectDescriptor.ListObjectDescriptorFactory: type argument 'System.Collections.Immutable.ImmutableArray`1[System.Uri]' violates the constraint of type parameter 'T'."


After submitting the issue

Please consider contributing to the project by submitting a PR with a fix to the issue.
This would help to solve your problem in a shorter time as well as help other users of the project.

In case you do not know where to start - feel free to ask for help in the issue thread.

Building an active community is essential for any project survival as time of maintainers is limited.

I believe this bug is related to the fact that ImmutableArray<T> is a struct.

The EnumerableObjectDescriptor .ListObjectDescriptorFactory<T, TValue> method (and similar methods for other collections) has a genetic constraint of where T : class, IList<TValue>.

Is there a reason for the class constraint?

This method was added in commit 855057b.

Hello @bsagal
No, I do not think there is any real reason to have a class constraint.