StubbleOrg / Stubble

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

Compiled renderer throws exception when compiling interpolated nullable int #134

Closed natehitze closed 1 year ago

natehitze commented 1 year ago

Occurs in version 1.10.8

Program.cs

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

var stubble = new StubbleBuilder().Build();
var model = new Model() { Count = 3 };

var templateString = "{{Count}}";
var output = stubble.Render(templateString, model);

// Outputs 3, 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);

// Should output 3 but Compile throws exception
Console.WriteLine("Output from compiled:");
Console.WriteLine(compiledOutput);

public class Model
{
    public int? Count { get; set; }
}

Exception thrown during Compile:

Unhandled exception. System.ArgumentException: Argument types do not match
   at System.Linq.Expressions.Expression.ValidateCoalesceArgTypes(Type left, Type right)
   at System.Linq.Expressions.Expression.Coalesce(Expression left, Expression right, LambdaExpression conversion)
   at System.Linq.Expressions.Expression.Coalesce(Expression left, Expression right)
   at Stubble.Compilation.Renderers.TokenRenderers.InterpolationTokenRenderer.Write(CompilationRenderer renderer, InterpolationToken obj, CompilerContext context)
   at Stubble.Core.Renderers.MustacheTokenRenderer`3.Write(RendererBase`1 renderer, MustacheToken obj, TContext context)
   at Stubble.Compilation.Renderers.CompilationRenderer.Render(BlockToken block, CompilerContext context)
   at Stubble.Compilation.Renderers.CompilationRenderer`1.Compile(MustacheTemplate document, CompilerContext compilationContext)
   at Stubble.Compilation.StubbleCompilationRenderer.Compile[T](String template, Type viewType, IDictionary`2 partials, CompilationSettings settings)
   at Stubble.Compilation.StubbleCompilationRenderer.Compile[T](String template)
Romanx commented 1 year ago

Thank you for submitting this and the excellent reproduction. While improving the safe null access we broke interpolation entirely, not sure how it worked before at all!

This has now been fixed so hopefully you shouldn't see it again, thank you for using the compilation renderer and we'll get a fixed version out shortly