insta icon indicating copy to clipboard operation
insta copied to clipboard

Redaction order seems to matter when using `sorted_redaction`

Open Threated opened this issue 1 year ago • 0 comments

What happened?

When using the sorted_redaction it seems to be important to place the sorting redactions at the end which I did not expect to matter. Also using [] as the selector instead of . also seems to fix the issue in the example below but does not seem to work if the redacted key value pair is nested somewhere deeper.

Reproduction steps

// insta = { version = "1.41.1", features = ["json", "redactions"] }
// rand = "*"
// serde_json = "*"
#[test]
fn insta_sorting_bug() {
    use insta::assert_json_snapshot;
    use rand::random;
    use serde_json::json;

    let json = json!([
        {
            "a": random::<bool>(),
            "z": 1,
        },
        {
            "a": random::<bool>(),
            "z": 2,
        }
    ]);
    // Inconsistent
    assert_json_snapshot!(json, {
        "." => insta::sorted_redaction(),
        "[].a" =>  "[redacted]",
    });
    // Consistent
    // assert_json_snapshot!(json, {
    //     "[].a" =>  "[redacted]",
    //     "." => insta::sorted_redaction(),
    // });
    // Consistent for this case but not for more deeply nested redacted values
    // assert_json_snapshot!(json, {
    //     "[]" => insta::sorted_redaction(),
    //     "[].a" =>  "[redacted]",
    // });
}

Insta Version

1.41.1

rustc Version

1.81

What did you expect?

Redaction order to not be important or its importance mentioned in the docs of sorted_redaction.

Threated avatar Nov 10 '24 14:11 Threated