Skip to content

Support for escaping curly braces in embedded expression #3962

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

bleaphar
Copy link
Contributor

@bleaphar bleaphar commented Jun 5, 2025

This pull request introduces support for escaped character sequences in HTTP syntax parsing, enabling the handling of complex scenarios such as escaped curly braces within variable values. Key changes include the addition of a new syntax node for escaped characters, updates to parsing logic, and corresponding tests.

Parsing Enhancements:

  • Added HttpEscapedCharacterSequenceNode to represent escaped character sequences in the syntax tree. This node trims escape characters (`This pull request introduces support for escaped character sequences in HTTP syntax parsing, enabling the handling of complex scenarios such as escaped curly braces within variable values. Key changes include the addition of a new syntax node for escaped characters, updates to parsing logic, and corresponding tests.

Parsing Enhancements:

) from the text. (src/Microsoft.DotNet.Interactive.Http.Parsing/Parsing/HttpEscapedCharacterSequenceNode.cs)

  • Updated HttpRequestParser to detect and parse escaped character sequences using the new ParseEscapedCharacterSequence method. (src/Microsoft.DotNet.Interactive.Http.Parsing/Parsing/HttpRequestParser.cs) [1] [2] [3] [4]

Syntax Tree Updates:

  • Modified HttpVariableValueNode to support adding HttpEscapedCharacterSequenceNode as a child node. (src/Microsoft.DotNet.Interactive.Http.Parsing/Parsing/HttpVariableValueNode.cs)
  • Enhanced HttpRootSyntaxNode to process escaped character sequences when resolving variable values, ensuring proper handling of escaped braces. (src/Microsoft.DotNet.Interactive.Http.Parsing/Parsing/HttpRootSyntaxNode.cs)

Testing:

  • Added unit tests to validate support for escaped curly braces and triple-escaped curly braces in variable values. (src/Microsoft.DotNet.Interactive.Http.Tests/ParserTests.Variables.cs)

var result = Parse(
"""

@text=\{{{text\}}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the escaping only intended to work on the RHS of variable assignment? Do we support literal values inside embedded expression within the body of requests for instance, and can those literal values contain {{ sequences that may need to be escaped?

(Sorry drawing a blank on how the embedded expressions are supposed to work within body and whether they only support references to other variables etc. :) If literal values are not supported within embedded expressions and only other variable references are supported there in, then feel free to ignore...)

++generationNumber;
yield return new object[]
{
new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection),
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you intend to repeat this section?

@@ -355,5 +380,64 @@ with XML.</description>
.Should().BeEquivalentTo("numberValue", "stringValue");
});
}

private static IEnumerable<HttpVariableDeclarationAndAssignmentNodeSyntaxSpec> ValidVariableDeclarations()
Copy link
Contributor

Choose a reason for hiding this comment

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

It might not be necessary to add so many cases. The goal of these tests is generally not to test every detail of the feature. The unit tests already cover that. They tend to be useful for catching issues with different combinations of syntax (e.g. does the parser move correctly from one node type to another) as well as incomplete syntax (e.g. does the parser handle it gracefully when the user is typing and the syntax is malformed in various normal ways.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants