From 038309fadab2fa47caa541ad3b286ebed948cb04 Mon Sep 17 00:00:00 2001 From: Eric Dahlvang Date: Sat, 6 Nov 2021 20:01:46 -0500 Subject: [PATCH] #minor CherryPick control property to 4.14 (#5956) * add control property (#5943) * add control property * disable fragile test * flip enableFromFile to false by default and fix unit tests (#5953) * flip enableFromFile to false by default and fix unit tests * fxi unit test Co-authored-by: johnataylor --- .../Evaluator.cs | 11 +++++++---- .../Expander.cs | 11 +++++++---- .../Templates.cs | 8 ++++++++ .../ActivityFactoryTests.cs | 2 ++ .../TemplatesTest.cs | 14 ++++++++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs index 2330dfc8fc..7f10550707 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs @@ -605,11 +605,14 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup) return new ExpressionEvaluator(template, FunctionUtils.Apply(this.TemplateFunction()), ReturnType.Object, this.ValidateTemplateFunction); } - const string fromFile = "fromFile"; - - if (name.Equals(fromFile, StringComparison.Ordinal)) + if (Templates.EnableFromFile) { - return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(FromFile()), ReturnType.String, ValidateFromFile); + const string fromFile = "fromFile"; + + if (name.Equals(fromFile, StringComparison.Ordinal)) + { + return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(FromFile()), ReturnType.String, ValidateFromFile); + } } const string activityAttachment = "ActivityAttachment"; diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs index 6b048fc731..4c401068d3 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs @@ -542,11 +542,14 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup, bo return new ExpressionEvaluator(template, FunctionUtils.Apply(this.TemplateFunction()), ReturnType.Object, this.ValidateTemplateFunction); } - const string fromFile = "fromFile"; - - if (name.Equals(fromFile, StringComparison.Ordinal)) + if (Templates.EnableFromFile) { - return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(this.FromFile()), ReturnType.String, ValidateFromFile); + const string fromFile = "fromFile"; + + if (name.Equals(fromFile, StringComparison.Ordinal)) + { + return new ExpressionEvaluator(fromFile, FunctionUtils.Apply(this.FromFile()), ReturnType.String, ValidateFromFile); + } } const string activityAttachment = "ActivityAttachment"; diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Templates.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Templates.cs index 0f63c240c1..d329cbc70b 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Templates.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Templates.cs @@ -77,6 +77,14 @@ public Templates( InjectToExpressionFunction(); } + /// + /// Gets or sets a value indicating whether fromFile is allowed in LG templates. + /// + /// + /// Boolean where true indicates fromFile is allowed. + /// + public static bool EnableFromFile { get; set; } = false; + /// /// Gets get all templates from current lg file and reference lg files. /// diff --git a/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Templates.Tests/ActivityFactoryTests.cs b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Templates.Tests/ActivityFactoryTests.cs index 1442e8cb2a..5eb7eaa657 100644 --- a/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Templates.Tests/ActivityFactoryTests.cs +++ b/tests/Microsoft.Bot.Builder.Dialogs.Adaptive.Templates.Tests/ActivityFactoryTests.cs @@ -20,6 +20,8 @@ public class ActivityFactoryTests public ActivityFactoryTests() { + Templates.EnableFromFile = true; + ComponentRegistration.Add(new DialogsComponentRegistration()); ComponentRegistration.Add(new DeclarativeComponentRegistration()); ComponentRegistration.Add(new AdaptiveComponentRegistration()); diff --git a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs index 28689124b9..98049c8df8 100644 --- a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs +++ b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs @@ -1818,6 +1818,8 @@ public void TestInjectLGWithoutNamespace() [Fact] public void TestFileOperation() { + Templates.EnableFromFile = true; + var templates = Templates.ParseFile(GetExampleFilePath("FileOperation.lg")); var evaluated = templates.Evaluate("FromFileWithoutEvaluation"); Assert.Equal("hi ${name}", evaluated); @@ -1832,6 +1834,18 @@ public void TestFileOperation() Assert.Equal("hi ${name}", evaluated); } + [Fact] + public void TestFileOperationDisabled() + { + Templates.EnableFromFile = false; + + Assert.Throws(() => + { + var templates = Templates.ParseFile(GetExampleFilePath("FileOperation.lg")); + var evaluated = templates.Evaluate("FromFileWithoutEvaluation"); + }); + } + public class LoopClass { public string Name { get; set; }