Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamicLinq gets confused on inconsistent JSON objects #104

Open
marc-weber1 opened this issue Nov 14, 2024 · 0 comments
Open

DynamicLinq gets confused on inconsistent JSON objects #104

marc-weber1 opened this issue Nov 14, 2024 · 0 comments
Assignees

Comments

@marc-weber1
Copy link

marc-weber1 commented Nov 14, 2024

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.

@StefH StefH self-assigned this Nov 14, 2024
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

No branches or pull requests

2 participants