Fix HandlebarsContext.ParseAndEvaluate (#1329)

This commit is contained in:
Stef Heyenrath
2025-07-12 11:05:02 +02:00
committed by GitHub
parent b0076b4e81
commit a06ee6b158
2 changed files with 11 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
// Copyright © WireMock.Net
using System.Text.RegularExpressions;
using HandlebarsDotNet;
using HandlebarsDotNet.Helpers.Extensions;
using Stef.Validation;
@@ -9,6 +10,8 @@ namespace WireMock.Transformers.Handlebars;
internal class HandlebarsContext : IHandlebarsContext
{
private static readonly Regex _tryEvaluateRegex = new(@"\{\{.*?\}\}", RegexOptions.Compiled);
public IHandlebars Handlebars { get; }
public IFileSystemHandler FileSystemHandler { get; }
@@ -27,9 +30,8 @@ internal class HandlebarsContext : IHandlebarsContext
public object? ParseAndEvaluate(string text, object model)
{
if (text.StartsWith("{{") && text.EndsWith("}}") &&
Handlebars.TryEvaluate(text, model, out var result) &&
result is not UndefinedBindingResult)
// Only try to evaluate if the text matches the pattern `{{ xxx }}` exactly once.
if (_tryEvaluateRegex.Matches(text).Count == 1 && Handlebars.TryEvaluate(text, model, out var result) && result is not UndefinedBindingResult)
{
return result;
}