Skip to content

ObjectToString has side-effect leading to incorrect escaping with lazy evaluation #634

@anya-hichu

Description

@anya-hichu

Hello,

While using scriban, I can across a problem with ObjectToString where the escaping is incorrect.

Issue reproduction

var script = """[["a", "b"]] | array.each @(do; ret $0 | array.join("#"); end)""";

var context = new TemplateContext();
var result = Scriban.Template.Evaluate(script, context);
Console.WriteLine(context.ObjectToString(result));

Actual output

["\"a\"#\"b\""]

Expected output

["a#b"]

I think it happens because context.ObjectToString is called inside array.join when evaluating so both the lazy evaluation and display logic is increasing _objectToStringLevel:

var item = context.ObjectToString(obj);

Proposed solution

Pass state as parameters to ObjectToString (defaults to 0) instead of using instance variables to avoid side-effects

private int _objectToStringLevel;
private int _currentToStringLength;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions