Handlebars.Net.Helpers icon indicating copy to clipboard operation
Handlebars.Net.Helpers copied to clipboard

DynamicLinq gets confused on inconsistent JSON objects

Open marc-weber1 opened this issue 1 year ago • 0 comments

Not sure if this is a DynamicLinq issue or an issue in the helper, hopefully you can forward this to the relevant repository since I'm not sure exactly how DynamicLinq is implemented here

Simple issue reproduction:

var data = JObject.Parse(@"
    {
        ""users"": [
            {
                ""name"": ""Emily"",
            },
            {
                ""name"": ""Jason"",
                ""phone_numbers"": [ ""+15555555555"" ]
            },
        ]
    }
");

string template_text = @"
{{#each (DynamicLinq.Where users 'it.name == ""Emily""')}}
<div>{{this.name}}</div>
{{/each}}
";

var handlebars = Handlebars.Create();
HandlebarsHelpers.Register(handlebars);
handlebars.Configuration.UseNewtonsoftJson();

var template = handlebars.Compile(template_text);
var rendered_text = template(data);

Debug.Assert(rendered_text.Trim() == "<div>Emily</div>");

Expected: assert passes, program exits with code 0

Observed:

System.InvalidOperationException
  HResult=0x80131509
  Message=The result of the dynamic binding produced by the object with type '<>f__AnonymousType0`1[System.String]' for the binder 'System.Linq.Dynamic.Core.DynamicGetMemberBinder' needs at least one restriction.
  Source=Handlebars.Net.Helpers
  StackTrace:
   at HandlebarsDotNet.Helpers.HandlebarsHelpers.InvokeMethod(Nullable`1 model, Boolean methodIsOnlyUsedInContextOfABlockHelper, IHandlebars context, String helperName, MethodInfo methodInfo, Arguments arguments, Object instance, IHelperOptions options)
   at HandlebarsDotNet.Helpers.HandlebarsHelpers.<>c__DisplayClass8_0.<RegisterValueHelper>b__0(HelperOptions& options, Context& context, Arguments& arguments)
   at HandlebarsDotNet.Helpers.DelegateReturnHelperWithOptionsDescriptor.Invoke(HelperOptions& options, Context& context, Arguments& arguments)
   at HandlebarsDotNet.Helpers.DelegateReturnHelperWithOptionsDescriptor.HandlebarsDotNet.Helpers.IHelperDescriptor<HandlebarsDotNet.HelperOptions>.Invoke(HelperOptions& options, Context& context, Arguments& arguments)
   at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass19_0.<Compile>b__0(TextWriter writer, Object context, Object data)
   at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass20_0.<Compile>b__0(Object context, Object data)
   at Program.<Main>$(String[] args) in C:\Users\mweber\Desktop\handlebars.net-handlers-test\Program.cs:line 33

Package versions:

    <PackageReference Include="Handlebars.Net" Version="2.1.6" />
    <PackageReference Include="Handlebars.Net.Extension.NewtonsoftJson" Version="1.0.3" />
    <PackageReference Include="Handlebars.Net.Helpers" Version="2.4.6" />
    <PackageReference Include="Handlebars.Net.Helpers.DynamicLinq" Version="2.4.6" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

Feel free to use this code as a new test.

marc-weber1 avatar Nov 14 '24 00:11 marc-weber1