Skip to content

Commit 8180fb6

Browse files
xoofxCopilot
andcommitted
Reset typed accessor cache with MemberFilter changes
Co-authored-by: Copilot <[email protected]>
1 parent 099cb04 commit 8180fb6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Scriban.Tests/TestRuntime.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,37 @@ public void TestScriptObjectAccessor()
10711071
Assert.False(accessor.HasMember(context, new SourceSpan(), obj, nameof(MyStaticObject.StaticPropertyA)));
10721072
Assert.False(accessor.HasMember(context, new SourceSpan(), obj, nameof(MyStaticObject.StaticPropertyB)));
10731073
}
1074+
1075+
// Test Reset clears cached typed accessors that captured the previous MemberFilter
1076+
{
1077+
var context = new TemplateContext
1078+
{
1079+
EnableRelaxedMemberAccess = false,
1080+
MemberFilter = _ => true
1081+
};
1082+
var obj = new MyObject { PropertyA = "allowed", PropertyB = "blocked" };
1083+
var template = Template.Parse("{{ model.property_b }}");
1084+
var accessor = context.GetMemberAccessor(obj);
1085+
var globals = new ScriptObject();
1086+
globals["model"] = obj;
1087+
context.PushGlobal(globals);
1088+
1089+
Assert.True(accessor.HasMember(context, new SourceSpan(), obj, "property_b"));
1090+
Assert.AreEqual("blocked", template.Render(context));
1091+
1092+
context.Reset();
1093+
context.MemberFilter = member => member.Name == nameof(MyObject.PropertyA);
1094+
1095+
accessor = context.GetMemberAccessor(obj);
1096+
Assert.False(accessor.HasMember(context, new SourceSpan(), obj, "property_b"));
1097+
1098+
globals = new ScriptObject();
1099+
globals["model"] = obj;
1100+
context.PushGlobal(globals);
1101+
1102+
var exception = Assert.Throws<ScriptRuntimeException>(() => template.Render(context));
1103+
StringAssert.Contains("Cannot get member", exception!.Message);
1104+
}
10741105
}
10751106

10761107
[Test]

src/Scriban/TemplateContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ public virtual void Reset()
901901
}
902902

903903
CachedTemplates.Clear();
904+
_memberAccessors.Clear();
904905
}
905906

906907
/// <summary>

0 commit comments

Comments
 (0)