Skip to content

Conversation

@JanRomero
Copy link

Since ToString() can return null, we must guard against this to prevent an ArgumentNullException later in WriteToAsync() when the string value gets encoded.

@sebastienros
Copy link
Owner

Can you provide an example that would trigger such exception in a template?

@JanRomero
Copy link
Author

@sebastienros sure, this may be contrived but this’ll do it:

public record struct MyModel(string Title, string? Value)
{
    public override string? ToString() => Value;
}
var template = fluidParser.Parse("""
        Title: {{MyItem.Title}}
        Value: {{MyItem}}        
        """);

var dict = new Dictionary<string, object>() { ["MyItem"] = new MyModel() };

template.Render(new TemplateContext(dict)); //System.ArgumentNullException: "Value cannot be null. Arg_ParamName_Name"

Visual Studio will actually generate string? ToString() if nullable types are enabled for the project.

@sebastienros
Copy link
Owner

I decided to accept null values but handle them, there might be other places that already return these.

#842

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 this pull request may close these issues.

2 participants