Skip to content

Commit e208c5b

Browse files
authored
Merge pull request #70 from robertwesterlund/ignore_static_properties_in_value_converter
LiquidValueConverter ignores static properties. Fix for #69.
2 parents 988bc1d + cef3c68 commit e208c5b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Liquid.NET.Tests/Utils/LiquidValueConverterTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,21 @@ public void It_Should_Convert_An_Object()
197197
Assert.Equal(LiquidString.Create((String) testClass.ObjField1), objAsHash["objfield1"].Value);
198198
}
199199

200+
[Fact]
201+
public void It_Should_Ignore_Static_Properties()
202+
{
203+
ClassWithStaticProperty.StaticField1 = "aaa";
204+
var testClass = new ClassWithStaticProperty { Field1 = "bbb" };
205+
206+
var result = _converter.Convert(testClass);
207+
Assert.True(result.HasValue);
208+
var objAsHash = (LiquidHash)result.Value;
209+
210+
Assert.Equal(1, objAsHash.Keys.Count);
211+
Assert.True(objAsHash.ContainsKey("field1"));
212+
Assert.Equal(LiquidString.Create(testClass.Field1), objAsHash["field1"].Value);
213+
}
214+
200215
[Fact]
201216
public void It_Should_Convert_A_Nested_Object()
202217
{
@@ -310,5 +325,10 @@ public class ClassWithAttributes
310325

311326
}
312327

328+
public class ClassWithStaticProperty
329+
{
330+
public String Field1 { get; set; }
331+
public static String StaticField1 { get; set; }
332+
}
313333
}
314334
}

Liquid.NET/src/Utils/LiquidValueConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Liquid.NET.Utils
1010
{
1111
public class LiquidValueConverter
1212
{
13+
public static readonly BindingFlags PublicInstancePropertiesWithAGetter = BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty;
14+
1315
public Option<ILiquidValue> Convert(Object obj)
1416
{
1517
if (ReferenceEquals(obj, null))
@@ -36,7 +38,7 @@ private Option<ILiquidValue> FromObject(Object obj)
3638
{
3739
var newHash = new LiquidHash();
3840
var kvps = obj.GetType()
39-
.GetProperties()
41+
.GetProperties(PublicInstancePropertiesWithAGetter)
4042
.Where(property => !(property.GetCustomAttributes<LiquidIgnoreAttribute>().Any()
4143
|| (property.GetCustomAttributes<LiquidIgnoreIfNullAttribute>().Any()
4244
&& ReferenceEquals(property.GetGetMethod().Invoke(obj, null), null))))

0 commit comments

Comments
 (0)