Skip to content
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

Compiled renderer evaluating int? truthy value incorrectly #116

Closed
natehitze opened this issue May 18, 2021 · 1 comment · Fixed by #132
Closed

Compiled renderer evaluating int? truthy value incorrectly #116

natehitze opened this issue May 18, 2021 · 1 comment · Fixed by #132
Milestone

Comments

@natehitze
Copy link

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 Romanx added this to the 1.10 milestone Aug 16, 2022
@Romanx
Copy link
Contributor

Romanx commented Aug 29, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants