Stubble icon indicating copy to clipboard operation
Stubble copied to clipboard

Compiled renderer evaluating int? truthy value incorrectly

Open natehitze opened this issue 4 years ago • 0 comments

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);
        }
    }
}

natehitze avatar May 18 '21 18:05 natehitze