StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
399 stars 58 forks source link

Compiled renderer evaluating int? truthy value incorrectly #116

Closed natehitze closed 2 years ago

natehitze commented 3 years ago

A null value is being treated as truthy. I can fix it by manually adding truthy check to the compilation builder's config.

Here's a repro Program.cs. I have Stubble.Core and Stubble.Compilation both installed at version 1.9.3.

using System;
using Stubble.Compilation.Builders;
using Stubble.Core.Builders;

namespace StubbleNullableIntRepro
{
    class Program
    {
        public class Model
        {
            public int? Count { get; set; }
        }

        static void Main(string[] args)
        {
            var stubble = new StubbleBuilder().Build();
            var model = new Model();

            var templateString = "{{#Count}}Should not be visible{{/Count}}";
            var output = stubble.Render(templateString, model);

            // Outputs nothing, as expected
            Console.WriteLine("Output from standard:");
            Console.WriteLine(output);

            var compiledStubble = new StubbleCompilationBuilder()
                .Build();
            var renderFunc = compiledStubble.Compile<Model>(templateString);
            var compiledOutput = renderFunc(model);

            // Outputs the template, not as expected
            Console.WriteLine("Output from compiled:");
            Console.WriteLine(compiledOutput);

            var fixedCompiledStubble = new StubbleCompilationBuilder()
                .Configure(cfg => cfg.AddTruthyCheck<int?>(input => input != null && input != 0))
                .Build();
            var fixedRenderFunc = fixedCompiledStubble.Compile<Model>(templateString);
            var fixedCompiledOutput = fixedRenderFunc(model);

            // Outputs nothing, as expected
            Console.WriteLine("Output from fixed compiled:");
            Console.WriteLine(fixedCompiledOutput);
        }
    }
}
Romanx commented 2 years ago

Hey @natehitze! Thanks for your patience this has now been released to nuget as v1.10.8

Thanks for using stubble and letting us know about this