Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DYN-1261 Escape StringInputNode value when converting NodeToCode #2

Closed
Closed
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
24 changes: 23 additions & 1 deletion src/DynamoCore/Engine/NodeToCode/NodeToCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,9 @@ internal static NodeToCodeResult NodeToCode(
// 4. Generate short name for long name variables. Typically they
// are from output ports from other nodes.
//
// 5. Do constant progation to optimize the generated code.
// Do constant progation to optimize the generated code.
//
// 5. Escape any \ characters in StringInputNode values
#region Step 1 AST compilation

AstBuilder builder = new AstBuilder(null);
Expand Down Expand Up @@ -1210,6 +1212,26 @@ internal static NodeToCodeResult NodeToCode(
}
#endregion

#region Step 5 Escape StringInputNodes
StudioLE marked this conversation as resolved.
Show resolved Hide resolved
foreach ((NodeModel nodeModel, IEnumerable<AssociativeNode> astNodes) in allAstNodes)
StudioLE marked this conversation as resolved.
Show resolved Hide resolved
{
if (nodeModel.NodeType != "StringInputNode")
StudioLE marked this conversation as resolved.
Show resolved Hide resolved
continue;

var astNode = astNodes.First();
if (!(astNode is BinaryExpressionNode astBinaryExpressionNode))
continue;

var rightNode = astBinaryExpressionNode.RightNode;
if (!(rightNode is StringNode astStringNode))
continue;

var escapedString = astStringNode.Value.Replace(@"\", @"\\")
.Replace("\"", "\\\"");
astStringNode.Value = escapedString;
}
#endregion

var result = new NodeToCodeResult(allAstNodes.SelectMany(x => x.Item2), inputMap, outputMap);
return result;
}
Expand Down
30 changes: 30 additions & 0 deletions test/DynamoCoreTests/NodeToCodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,36 @@ public void TestNodeToCodeUndoRecorder()
Assert.AreEqual(0, recorder.ActionCount());
}

[Test]
public void TestNodeToCodeStringInputEscaping()
StudioLE marked this conversation as resolved.
Show resolved Hide resolved
{
// Arrange
OpenModel(@"core\node2code\stringNodesInNeedOfEscaping.dyn");
var nodes = CurrentDynamoModel.CurrentWorkspace.Nodes;
var engine = CurrentDynamoModel.EngineController;

// Act
var nodesToCode = engine.ConvertNodesToCode(nodes, nodes);
var results = nodesToCode.AstNodes.OfType<BinaryExpressionNode>()
.Where((x, i) => i < 8)
.Select(x => x.RightNode.ToString())
.ToList();

// Assert
var expect = new List<string>
Copy link
Collaborator

@SHKnudsen SHKnudsen Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, makes sense now!

If we are really picky the expect list should probably go into the Arrange section

{
"\"C:\\\\\"", // "C:\\"
"\"4\\\"\"", // "4\"
"\"\\\"Hello, world.\\\"\"", // "\"Hello, world.\""
"\"Hello\\\\r\\\\nWorld\"", // "Hello\\r\\nWorld"
"\"\\\\tHello World\"", // "\\tHello World"
"\"\\\\u33A1\"", // "\\u33A1"
"\"\\\\u00B2\"", // "\\u00B2"
"\"\\\\\\\\SERVER\\\\PATH\"" // "\\\\SERVER\\PATH"
};
Assert.AreEqual(expect, results);
}

[Test]
public void TestUINode_String()
{
Expand Down
Loading