Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions source/Handlebars.Test/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,56 @@ public void ElseWithCustomBlockHelperInvocationChainsThreeDeep()
Assert.Equal("D", template(new { value = "z" }));
}

public interface Issue601_IBaseData
{
DateTime DateTimeUtc { get; }
}

public interface Issue601_IData : Issue601_IBaseData
{
string DateTimeStr => DateTimeUtc.ToString("O");
}

public class Issue601_Data : Issue601_IData
{
public DateTime DateTimeUtc { get; set; }
public string OtherStr => DateTimeUtc.ToString("D");
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/601
// A property whose only implementation is a C# 8+ default interface member (declared and
// bodied on the interface, not overridden by the concrete class) must still be resolvable
// by name, even though it's invisible to Type.GetProperties() on the concrete class.
[Fact]
public void Issue601_DefaultInterfaceMemberPropertyIsResolved()
{
var handlebars = Handlebars.Create();
var template = handlebars.Compile("{{DateTimeStr}}|{{OtherStr}}");

var data = new Issue601_Data { DateTimeUtc = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Utc) };
Issue601_IData dataAsInterface = data;
var result = template(data);

Assert.Equal($"{dataAsInterface.DateTimeStr}|{data.OtherStr}", result);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/601
// Enumerating a POCO's members (e.g. via {{#each this}}) must also surface
// default-interface-member-backed properties, not just class-declared ones.
[Fact]
public void Issue601_DefaultInterfaceMemberPropertyIsEnumerated()
{
var handlebars = Handlebars.Create();
var template = handlebars.Compile("{{#each this}}{{@key}}={{this}};{{/each}}");

var data = new Issue601_Data { DateTimeUtc = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Utc) };
Issue601_IData dataAsInterface = data;
var result = template(data);

Assert.Contains($"DateTimeStr={dataAsInterface.DateTimeStr};", result);
Assert.Contains($"OtherStr={data.OtherStr};", result);
}

private static void RegisterStringEqualityBlockHelper(IHandlebars handlebars)
{
handlebars.RegisterHelper("StringEqualityBlockHelper", (output, options, context, arguments) =>
Expand Down
9 changes: 9 additions & 0 deletions source/Handlebars/MemberAccessors/ReflectionMemberAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ public RawObjectTypeDescriptor(Type type)
string.Equals(o.Name, name.LowerInvariant, StringComparison.OrdinalIgnoreCase)
);

// Properties implemented purely as C# 8+ default interface members (no override on the
// concrete class) don't appear via reflection on the class itself, only on the interface.
property ??= type.GetInterfaces()
.SelectMany(o => o.GetProperties(BindingFlags.Instance | BindingFlags.Public))
.FirstOrDefault(o =>
o.GetIndexParameters().Length == 0 &&
string.Equals(o.Name, name.LowerInvariant, StringComparison.OrdinalIgnoreCase)
);

if (property != null)
{
return (Func<object, object>) CreateGetDelegateMethodInfo
Expand Down
10 changes: 9 additions & 1 deletion source/Handlebars/ObjectDescriptors/ObjectDescriptorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ public bool TryGetDescriptor(Type type, [NotNullWhen(true)] out ObjectDescriptor
{
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(o => o.CanRead && o.GetIndexParameters().Length == 0);


// Properties implemented purely as C# 8+ default interface members (no override on the
// concrete class) don't appear via reflection on the class itself, only on the interface.
var interfaceProperties = type.GetInterfaces()
.SelectMany(o => o.GetProperties(BindingFlags.Public | BindingFlags.Instance))
.Where(o => o.CanRead && o.GetIndexParameters().Length == 0);

var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
return properties
.Concat(interfaceProperties)
.Cast<MemberInfo>()
.Concat(fields)
.Select(o => o.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.Select(o => ChainSegment.Create(o))
.ToArray();
});
Expand Down
Loading