This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 451
Support for escaping curly braces in embedded expression #3962
Open
bleaphar
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
bleaphar:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
639e4f9
Support for escaping curly braces in embedded expression
bleaphar 3bce799
Merge branch 'dotnet:main' into main
bleaphar 31d8469
Better tests nomenclature and nit
bleaphar 0c44d92
Variable combinatorials
bleaphar 4fdf463
Update HttpRequestNodeSyntaxSpec.cs
bleaphar ef14f09
Nixed repeat section
bleaphar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Microsoft.DotNet.Interactive.Http.Parsing/Parsing/HttpEscapedCharacterSequenceNode.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #nullable enable | ||
|
|
||
| using Microsoft.CodeAnalysis.Text; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Microsoft.DotNet.Interactive.Http.Parsing; | ||
| internal class HttpEscapedCharacterSequenceNode : HttpSyntaxNode | ||
| { | ||
| public HttpEscapedCharacterSequenceNode(SourceText sourceText, HttpSyntaxTree syntaxTree) : base(sourceText, syntaxTree) | ||
| { | ||
| } | ||
|
|
||
| public string UnescapedText => Text.TrimStart('\\'); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,7 @@ public static IEnumerable<object[]> GenerateValidRequests() | |
| var generationNumber = 0; | ||
|
|
||
| foreach(var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
|
|
@@ -115,7 +116,7 @@ public static IEnumerable<object[]> GenerateValidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
@@ -125,6 +126,7 @@ public static IEnumerable<object[]> GenerateValidRequestsWithExtraTrivia() | |
| { | ||
| var generationNumber = 0; | ||
|
|
||
| foreach(var variables in ValidVariableDeclarations()) | ||
| foreach (var namedRequest in ValidNamedRequests()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
|
|
@@ -135,7 +137,7 @@ public static IEnumerable<object[]> GenerateValidRequestsWithExtraTrivia() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection) | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection) | ||
| { | ||
| Randomizer = new Random(1) | ||
| }, | ||
|
|
@@ -149,6 +151,7 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| var generationNumber = 0; | ||
|
|
||
| foreach (var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in InvalidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
|
|
@@ -158,12 +161,13 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
||
| foreach (var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in InvalidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
|
|
@@ -173,12 +177,13 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
||
| foreach(var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in InvalidVersions()) | ||
|
|
@@ -188,12 +193,13 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
||
| foreach(var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
|
|
@@ -203,12 +209,14 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| foreach (var namedRequest in InvalidNamedRequests()) | ||
| foreach (var variables in ValidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
|
|
@@ -218,11 +226,28 @@ public static IEnumerable<object[]> GenerateInvalidRequests() | |
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, method, url, version, headerSection, bodySection), | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
|
|
||
| foreach (var namedRequest in ValidNamedRequests()) | ||
| foreach (var variables in InvalidVariableDeclarations()) | ||
| foreach (var method in ValidMethods()) | ||
| foreach (var url in ValidUrls()) | ||
| foreach (var version in ValidVersions()) | ||
| foreach (var headerSection in ValidHeaderSections()) | ||
| foreach (var bodySection in ValidBodySections()) | ||
| { | ||
| ++generationNumber; | ||
| yield return new object[] | ||
| { | ||
| new HttpRequestNodeSyntaxSpec(namedRequest, variables, method, url, version, headerSection, bodySection), | ||
| generationNumber | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<HttpMethodNodeSyntaxSpec> ValidMethods() | ||
| { | ||
|
|
@@ -355,5 +380,64 @@ with XML.</description> | |
| .Should().BeEquivalentTo("numberValue", "stringValue"); | ||
| }); | ||
| } | ||
|
|
||
| private static IEnumerable<HttpVariableDeclarationAndAssignmentNodeSyntaxSpec> ValidVariableDeclarations() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.) |
||
| { | ||
| yield return null; | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@host=localhost"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@host=https://example.com"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@api_key=secret123"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@base_url=https://{{host}}/api", node => | ||
| { | ||
| node.ValueNode.DescendantNodesAndTokens().OfType<HttpEmbeddedExpressionNode>() | ||
| .Should().ContainSingle() | ||
| .Which.ExpressionNode.Text.Should().Be("host"); | ||
| }); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@escaped=\\{\\{text\\}\\}", node => | ||
| { | ||
| node.ValueNode.Text.Should().Be("{{text}}"); | ||
| }); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@with_spaces=one two three"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@quoted=\"hello world\""); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@single_quoted='hello world'"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@user.name=john_doe"); | ||
|
|
||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@dynamic={{$guid}}", node => | ||
| { | ||
| node.ValueNode.DescendantNodesAndTokens().OfType<HttpEmbeddedExpressionNode>() | ||
| .Should().ContainSingle() | ||
| .Which.ExpressionNode.Text.Should().Be("$guid"); | ||
| }); | ||
| } | ||
|
|
||
| private static IEnumerable<HttpVariableDeclarationAndAssignmentNodeSyntaxSpec> InvalidVariableDeclarations() | ||
| { | ||
| // Missing variable name | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@=value"); | ||
|
|
||
| // Variable name starting with number | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@123invalid=value"); | ||
|
|
||
| // Invalid character in variable name (hyphen) | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@my-var=value"); | ||
|
|
||
| // Space in variable name | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@var name=value"); | ||
|
|
||
| // Missing equals sign | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@host value"); | ||
|
|
||
| // Special characters in variable name | ||
| yield return new HttpVariableDeclarationAndAssignmentNodeSyntaxSpec("@host!name=value"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?