Skip to content

Commit

Permalink
Updated Template (#7485) (#7487)
Browse files Browse the repository at this point in the history
* updated template

* updated test
  • Loading branch information
jwoo-msft authored Jun 1, 2022
1 parent 732549e commit 6cb5593
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdaptiveExpressions.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace AdaptiveCards.Templating
{
Expand Down Expand Up @@ -37,9 +39,16 @@ public bool TryGetValue(string path, out object value)
bool result = simpleObjectMemory.TryGetValue(path, out value);
if (value is string)
{
string serializedValue = JsonConvert.SerializeObject(value);
// after serialization, the double quotes should be removed
value = serializedValue.Substring(1, serializedValue.Length - 2);
try
{
JToken jsonObject = JToken.Parse(value as string);
}
catch (JsonReaderException)
{
string serializedValue = JsonConvert.SerializeObject(value);
// after serialization, the double quotes should be removed
value = serializedValue.Substring(1, serializedValue.Length - 2);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private void PushTemplatedDataContext(string jpath)
throw new ArgumentNullException("Parent data context or selection path is null");
}

var (value, error) = new ValueExpression("=" + jpath).TryGetValue(parentDataContext.AELMemory);
var (value, error) = new ValueExpression("=" + Regex.Unescape(jpath)).TryGetValue(parentDataContext.AELMemory);
if (error == null)
{
var serializedValue = JsonConvert.SerializeObject(value);
Expand Down
125 changes: 125 additions & 0 deletions source/dotnet/Test/AdaptiveCards.Templating.Test/TestTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13342,6 +13342,131 @@ public void TestWhenExpressionNotInDataWithLog()

Assert.AreEqual(expectedWarning, log[0]);
}

[TestMethod]
public void TestJPathOnData()
{
string cardJson =
@"{
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""type"": ""AdaptiveCard"",
""version"": ""1.3"",
""body"": [
{
""type"": ""TextBlock"",
""text"": ""${string(jPath(FormMetaData, \""$..FieldOptions[?(@.Name == 'Title')].IsReadOnly\""))}""
},
{
""type"": ""Container"",
""$when"": ""${not(jPath(FormMetaData, \""$..FieldOptions[?(@.Name == 'Severity')].IsReadOnly\""))}"",
""items"": [
{
""type"": ""Input.ChoiceSet"",
""id"": ""Severity"",
""style"": ""expanded"",
""choices"": [
{
""$data"": ""${jPath(FormMetaData, \""$..FieldOptions[?(@.Name == 'Severity')].Options\"")}"",
""title"": ""${Title}"",
""value"": ""${Value}""
}
],
""label"": ""Severity of the Incident"",
""isRequired"": true,
""errorMessage"": ""Severity of the Incident""
}
]
}
]
}";

string expectedJson =
@"{
""$schema"": ""http://adaptivecards.io/schemas/adaptive-card.json"",
""type"": ""AdaptiveCard"",
""version"": ""1.3"",
""body"": [
{
""type"": ""TextBlock"",
""text"": ""false""
},
{
""type"": ""Container"",
""items"": [
{
""type"": ""Input.ChoiceSet"",
""id"": ""Severity"",
""style"": ""expanded"",
""choices"": [
{
""title"": ""1"",
""value"": ""1""
},
{
""title"": ""2"",
""value"": ""2""
},
{
""title"": ""3"",
""value"": ""3""
},
{
""title"": ""4"",
""value"": ""4""
}
],
""label"": ""Severity of the Incident"",
""isRequired"": true,
""errorMessage"": ""Severity of the Incident""
}
]
}
]
}";

var context = new EvaluationContext()
{
Root =
@"{
""Title"": ""Issue with "",
""FormMetaData"": {
""FieldOptions"": [
{
""Name"": ""Title"",
""IsReadOnly"": false,
""Options"": []
},
{
""Name"": ""Severity"",
""IsReadOnly"": false,
""Options"": [
{
""Title"": ""1"",
""Value"": ""1""
},
{
""Title"": ""2"",
""Value"": ""2""
},
{
""Title"": ""3"",
""Value"": ""3""
},
{
""Title"": ""4"",
""Value"": ""4""
}
]
}
]
}
}"
};

var template = new AdaptiveCardTemplate(cardJson);
string st = template.Expand(context);
AssertJsonEqual(expectedJson, st);
}
}
[TestClass]
public sealed class TestRootKeyword
Expand Down

0 comments on commit 6cb5593

Please sign in to comment.