mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 17:41:01 +01:00
Fix HandlebarsContext.ParseAndEvaluate (#1329)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user