DYN-1261 Escape StringInputNode value when converting NodeToCode#11295
Conversation
|
I reran this job and tests all pass - not sure why the checks still show a failure. |
| #region Step 5 Escape StringInputNodes | ||
| foreach ((NodeModel nodeModel, IEnumerable<AssociativeNode> astNodes) in allAstNodes) | ||
| { | ||
| if (nodeModel.NodeType != "StringInputNode") |
There was a problem hiding this comment.
Using the type name StringInputNode here can be prone to breaking if it's changed in the future for some reason and hard to debug. I would suggest using the following check instead:
if(!(nodeModel is CoreNodeModels.Input.StringInput)) {...}
There was a problem hiding this comment.
I don't believe it's possible to reference CoreNodeModels.Input.StringInput from the DynamoCore project as DynamoCore is itself a reference of CoreNodeModels so it would lead to a circular dependency.
Is there a different way to identify the StringInput node?
There was a problem hiding this comment.
Sorry, I missed that. In that case, I would suggest you use the Visitor pattern we have several classes for. I'd suggest you add a new class like the following:
private class StringReplacer : AstReplacer
{
public override AssociativeNode VisitStringNode(StringNode node)
{
var escapedString = node.Value.Replace(@"\", @"\\")
.Replace("\"", "\\\"");
node.Value = escapedString;
return node;
}
}
Then call this in place of your code above like this:
#region Step 5 Escape StringInputNodes
foreach ((NodeModel nodeModel, IEnumerable<AssociativeNode> astNodes) in allAstNodes)
{
var stringReplacer = new StringReplacer();
stringReplacer.VisitNodeList(astNodes.ToList());
}
#endregion
There was a problem hiding this comment.
You can make StringReplacer a private nested class within NodeToCodeCompiler since it's most likely going to be used only in the node to code context.
There was a problem hiding this comment.
Thanks @aparajit-pratap
I tried implementing the Visitor method but it led to the following tests failing:
This seems to occur as nodes like the DirectoryPath also trigger the StringReplacer.VisitStringNode method which leads to them becoming escaped twice. For the path C:\ you would expect C:\\ But instead you get C:\\\\
It seems the Directory Path is already escaped in the BuildAst method:
Dynamo/src/Libraries/CoreNodeModels/Input/FileSystem.cs
Lines 49 to 55 in b3b5c09
As this looks like a cleaner solution I've adopted the same approach for the StringInput node in commit c39adef and reverted the earlier changes I made to NodeToCode.cs. Please let me know your thoughts on this?
aparajit-pratap
left a comment
There was a problem hiding this comment.
@StudioLE this looks great. Thanks for proposing the new solution. I had tried exploring changing BuildAst but I didn't remember to check for NodeToCode context.
) (#11326) * Escape StringInputNode value when converting NodeToCode * Updated workflow comment * Added NodeToCode StringInput escaping test * Restructured NodeToCode StringInput escaping test * Escape StringInput value when compiling NodeToCode Co-authored-by: Laurence Elsdon <[email protected]>

Purpose
JIRA: DYN-1261
Related Issues:
Ensure that string containing
"and\are correctly escaped when convertingStringInputNodeusing NodeToCode.Behaviour before:
Behaviour after:

Declarations
Check these if you believe they are true
*.resxfilesReviewers
@mjkkirschner @QilongTang @mmisol
FYIs
@Amoursol