Skip to content

IndexOutOfRangeException in HandlebarsDotNet.PathStructure.PathResolver.ResolvePath for nested object path block expression #442

Closed

Description

@swijnands

Describe the bug

Trying to enumerate an array via a nested object path:

{{#bundles.styles.vendor}}{{this}}{{/bundles.styles.vendor}}

Results in an IndexOutOfRangeException exception.

Expected behavior:

I would expect the same result as when not using a nested path, i.e.

{{#arr}}....{/arr}}

Or, alternatively, if this is not supported: a clearer exception message.

Test to reproduce

var context = new
{
  bundles = new
  {
	  styles = new
	  {
		  vendor = new[] { "a", "b", "c" }
	  }
  },
  arr = new[] { "d", "e", "f"}
};

// Works: Output = "abc"
Handlebars.Compile("{{#each bundles.styles.vendor}}{{this}}{{/each}}")(context).Dump();

// Works: Output = "def"
Handlebars.Compile("{{#arr}}{{this}}{{/arr}}")(context).Dump();

// Fails: IndexOutOfRangeException in HandlebarsDotNet.PathStructure.PathResolver.ResolvePath
// On line #36: if (!TryResolveValue(pathInfo.IsVariable, context, pathChain[0], instance, out instance))
Handlebars.Compile("{{#bundles.styles.vendor}}{{this}}{{/bundles.styles.vendor}}")(context).Dump();```

Activity

  1. oformaniuk commented on Mar 24, 2021

    @oformaniuk
    Member

    This should definitely be supported.
    Looks like something weird is happening when we're trying to resolve an actual array to enumerate on.

  2. sushantduggal commented on Jun 29, 2021

    @sushantduggal

    Facing the same issue, is there a resolution on this?

  3. devzaak commented on Aug 27, 2021

    @devzaak

    I am not 100% sure but this could be a fix for issue:
    In: PathResolver.cs
    replacing

                var segments = pathInfo.Segments;

    with

                var segments = pathInfo.Segments.Where(segment => segment.IsNotEmpty).ToArray();

    I am unsure if this will cause other problems though as I am not familiar with lib yet.

  4. oformaniuk commented on Aug 27, 2021

    @oformaniuk
    Member

    @aslezak , it's hard to tell without testing. I'd really appreciate if you can have a look closer.

    nit: Try to avoid Linq as this is performance critical path. In this case the check can be placed inside of for that is right below the line you mentioned.

  5. devzaak commented on Sep 1, 2021

    @devzaak

    Will do as soon as I have some spare time, and yes, I noticed I can do continue; in the loop instead of prefiltering them.

  6. added this to the v3 milestone on Dec 22, 2021
  7. abraham-fox commented on Jan 23, 2022

    @abraham-fox
    Contributor

    No updates for 5 month - I assume I can grab this :) and it seems I've found a simple solution for the issue. @zjklee please take a look at #488.

  8. linked a pull request that will close this issueEnable support for chained path iterators #488on Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions