dotliquid / dotliquid

.NET Port of Tobias Lütke's Liquid template language.
https://www.dotliquid.org
Other
1.06k stars 296 forks source link

IndexOutOfRangeException #531

Open davide1993 opened 1 year ago

davide1993 commented 1 year ago

Dotliquid version

2.2.508

Expected behavior

value=null or value=string.Empty

Actual behavior

throw IndexOutOfRangeException

Steps to reproduce the Problem (you can add files)

TryEvaluateHashOrArrayLikeObject methods in Context class does not check if the index is a valid key and throws IndexOutOfRangeException.

image

microalps commented 1 year ago

Please provide a template proof of concept or a test case to assist getting a resolution quickly. Based on the report, it seems you are accessing index > size of list or index < negative size of list.

davide1993 commented 1 year ago

You are right. This is my template myArray[0].myfield where myArray is empty. I want to resolve that field but when arrayIndex>array.length it throws an exception and the user see "Errore Liquid: Index was outside the bounds of the array." so I suggest to add an if condition for this scenario and set value to null or string.Empty.

microalps commented 5 months ago

so I suggest to add an if condition for this scenario and set value to null or string.Empty.

Do you have any reference test that indicates this is the behavior on Ruby Liquid? And if yes, which is it, null or empty? A test on replit would suffice.

microalps commented 5 hours ago

See https://replit.com/@microalps/LiquidUnitTests#main.rb test named test_empty_array_index that indicates it returns an empty string. However, see also test_array_indexoutofbound that indicates this is even true when the array is of integers which might not be desired. @daviburg thoughts?