-
-
Notifications
You must be signed in to change notification settings - Fork 187
Add support for identifiers ending with ? in Liquid templates
#841
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
Merged
sebastienros
merged 4 commits into
main
from
copilot/support-identifiers-ending-with-questionmark
Oct 23, 2025
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9653ad1
Initial plan
Copilot 82f1d28
Add AllowTrailingQuestion option to support identifiers ending with ?
Copilot 1524842
Add tests for intermediate identifiers with trailing question marks
Copilot 7f966c4
Rename AllowTrailingQuestion to AllowTrailingQuestionMark
Copilot 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
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,295 @@ | ||
| using Fluid.Ast; | ||
| using Fluid.Parser; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Fluid.Tests | ||
| { | ||
| public class TrailingQuestionTests | ||
| { | ||
| [Fact] | ||
| public void ShouldNotParseTrailingQuestionByDefault() | ||
| { | ||
| var parser = new FluidParser(); | ||
| var result = parser.TryParse("{{ product.empty? }}", out var template, out var errors); | ||
|
|
||
| Assert.False(result); | ||
| Assert.NotNull(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionWhenEnabled() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{{ product.empty? }}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldStripTrailingQuestionFromIdentifier() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ product.empty? }}", out var template, out var errors); | ||
|
|
||
| var statements = ((FluidTemplate)template).Statements; | ||
| var outputStatement = statements[0] as OutputStatement; | ||
| Assert.NotNull(outputStatement); | ||
|
|
||
| var memberExpression = outputStatement.Expression as MemberExpression; | ||
| Assert.NotNull(memberExpression); | ||
| Assert.Equal(2, memberExpression.Segments.Count); | ||
|
|
||
| var firstSegment = memberExpression.Segments[0] as IdentifierSegment; | ||
| Assert.NotNull(firstSegment); | ||
| Assert.Equal("product", firstSegment.Identifier); | ||
|
|
||
| var secondSegment = memberExpression.Segments[1] as IdentifierSegment; | ||
| Assert.NotNull(secondSegment); | ||
| Assert.Equal("empty", secondSegment.Identifier); // Should NOT contain '?' | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldResolveIdentifierWithoutQuestionMark() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ products.empty? }}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| var sampleObj = new { empty = true }; | ||
| context.Options.MemberAccessStrategy.Register(sampleObj.GetType()); | ||
| context.SetValue("products", sampleObj); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("true", result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{{ a? }}", "a")] | ||
| [InlineData("{{ product.quantity_price_breaks_configured? }}", "quantity_price_breaks_configured")] | ||
| [InlineData("{{ collection.products.empty? }}", "empty")] | ||
| public void ShouldStripTrailingQuestionFromVariousIdentifiers(string template, string expectedLastSegment) | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse(template, out var parsedTemplate, out var errors); | ||
|
|
||
| var statements = ((FluidTemplate)parsedTemplate).Statements; | ||
| var outputStatement = statements[0] as OutputStatement; | ||
| Assert.NotNull(outputStatement); | ||
|
|
||
| var memberExpression = outputStatement.Expression as MemberExpression; | ||
| Assert.NotNull(memberExpression); | ||
|
|
||
| var lastSegment = memberExpression.Segments[^1] as IdentifierSegment; | ||
| Assert.NotNull(lastSegment); | ||
| Assert.Equal(expectedLastSegment, lastSegment.Identifier); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionInIfStatement() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{% if collection.products.empty? %}No products{% endif %}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldEvaluateTrailingQuestionInIfStatement() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{% if collection.products.empty? %}No products{% endif %}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| var products = new { empty = true }; | ||
| var collection = new { products }; | ||
| context.Options.MemberAccessStrategy.Register(products.GetType()); | ||
| context.Options.MemberAccessStrategy.Register(collection.GetType()); | ||
| context.SetValue("collection", collection); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("No products", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionInFilterArgument() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{{ value | filter: item.empty? }}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionInAssignment() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{% assign x = product.empty? %}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldEvaluateTrailingQuestionInAssignment() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{% assign x = product.empty? %}{{ x }}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| var product = new { empty = false }; | ||
| context.Options.MemberAccessStrategy.Register(product.GetType()); | ||
| context.SetValue("product", product); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("false", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldNotAllowMultipleTrailingQuestions() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{{ product.empty?? }}", out var template, out var errors); | ||
|
|
||
| // Should fail because we only allow one trailing question mark | ||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionInForLoop() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{% for item in collection.items? %}{{ item }}{% endfor %}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldWorkWithMixedIdentifiers() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ a.b? }}{{ c.d }}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| var objA = new { b = "value1" }; | ||
| var objC = new { d = "value2" }; | ||
| context.Options.MemberAccessStrategy.Register(objA.GetType()); | ||
| context.Options.MemberAccessStrategy.Register(objC.GetType()); | ||
| context.SetValue("a", objA); | ||
| context.SetValue("c", objC); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("value1value2", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldParseTrailingQuestionWithIndexer() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse("{{ items[0].empty? }}", out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{{ a? | upcase }}")] | ||
| [InlineData("{{ a.b? | append: '.txt' }}")] | ||
| public void ShouldParseTrailingQuestionWithFilters(string templateText) | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| var result = parser.TryParse(templateText, out var template, out var errors); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Null(errors); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldRenderTrailingQuestionWithFilters() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ text | upcase }}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| context.SetValue("text", "hello"); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("HELLO", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldSupportTrailingQuestionOnIntermediateIdentifiers() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ a?.b.c }}", out var template, out var errors); | ||
|
|
||
| var statements = ((FluidTemplate)template).Statements; | ||
| var outputStatement = statements[0] as OutputStatement; | ||
| Assert.NotNull(outputStatement); | ||
|
|
||
| var memberExpression = outputStatement.Expression as MemberExpression; | ||
| Assert.NotNull(memberExpression); | ||
| Assert.Equal(3, memberExpression.Segments.Count); | ||
|
|
||
| var firstSegment = memberExpression.Segments[0] as IdentifierSegment; | ||
| Assert.NotNull(firstSegment); | ||
| Assert.Equal("a", firstSegment.Identifier); // Should NOT contain '?' | ||
|
|
||
| var secondSegment = memberExpression.Segments[1] as IdentifierSegment; | ||
| Assert.NotNull(secondSegment); | ||
| Assert.Equal("b", secondSegment.Identifier); | ||
|
|
||
| var thirdSegment = memberExpression.Segments[2] as IdentifierSegment; | ||
| Assert.NotNull(thirdSegment); | ||
| Assert.Equal("c", thirdSegment.Identifier); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ShouldResolveIntermediateIdentifiersWithTrailingQuestion() | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse("{{ obj?.nested.value }}", out var template, out var errors); | ||
|
|
||
| var context = new TemplateContext(); | ||
| var nested = new { value = "test" }; | ||
| var obj = new { nested }; | ||
| context.Options.MemberAccessStrategy.Register(nested.GetType()); | ||
| context.Options.MemberAccessStrategy.Register(obj.GetType()); | ||
| context.SetValue("obj", obj); | ||
|
|
||
| var result = await template.RenderAsync(context); | ||
| Assert.Equal("test", result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("{{ a?.b.c }}", new[] { "a", "b", "c" })] | ||
| [InlineData("{{ a.b?.c }}", new[] { "a", "b", "c" })] | ||
| [InlineData("{{ a?.b?.c }}", new[] { "a", "b", "c" })] | ||
| [InlineData("{{ a?.b?.c? }}", new[] { "a", "b", "c" })] | ||
| public void ShouldStripTrailingQuestionFromAllSegments(string template, string[] expectedIdentifiers) | ||
| { | ||
| var parser = new FluidParser(new FluidParserOptions { AllowTrailingQuestion = true }); | ||
| parser.TryParse(template, out var parsedTemplate, out var errors); | ||
|
|
||
| var statements = ((FluidTemplate)parsedTemplate).Statements; | ||
| var outputStatement = statements[0] as OutputStatement; | ||
| Assert.NotNull(outputStatement); | ||
|
|
||
| var memberExpression = outputStatement.Expression as MemberExpression; | ||
| Assert.NotNull(memberExpression); | ||
| Assert.Equal(expectedIdentifiers.Length, memberExpression.Segments.Count); | ||
|
|
||
| for (int i = 0; i < expectedIdentifiers.Length; i++) | ||
| { | ||
| var segment = memberExpression.Segments[i] as IdentifierSegment; | ||
| Assert.NotNull(segment); | ||
| Assert.Equal(expectedIdentifiers[i], segment.Identifier); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.
@copilot rename to
AllowTrailingQuestionMarkThere 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.
Renamed to
AllowTrailingQuestionMarkin commit 7f966c4. Updated all occurrences in FluidParserOptions.cs, FluidParser.cs, and all test files.