Skip to content

Ensure ObjectValue.ToStringValue() doesn’t return null #777

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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