Skip to content
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

Update readme.md #696

Merged
merged 1 commit into from
Dec 17, 2024
Merged

Update readme.md #696

merged 1 commit into from
Dec 17, 2024

Conversation

STRUDSO
Copy link
Contributor

@STRUDSO STRUDSO commented Dec 16, 2024

Changed scrub xml and scrub json to serializer and moved them up together with the other serializer snippets.

Also a question was trying to play around with this for mvc problem details, got a traceid as part of output.
I could perhaps do a regex to match a traceid, but would rather not...
I also tried to just manipulate the object like so:

 var content = await result.Content.ReadFromJsonAsync<ProblemDetails>();

            Assert.NotNull(content);
            content.Extensions["traceId"] = "[any trace...]";

            InlineSnapshot
                .Validate

so my solution ended being this

 var content = await result.Content.ReadFromJsonAsync<ProblemDetails>();
            InlineSnapshot
                .WithSettings(x => x.ScrubLinesWithReplace(
                    x => x.Contains("traceid", StringComparison.OrdinalIgnoreCase) ? "  traceId: [any trace...]" : x
                    ))
                .Validate(content, """
                    Title: Validation Error
                    Status: 400
                    Detail:
                      Validation failed: 
                       -- PageNumber: 'Page Number' must be greater than '0'. Severity: Error
                    Instance: GET /meetinggroupproposals
                    Extensions:
                      traceId: [any trace...]
                      Errors:
                        [
                          {
                            "key": "PageNumber",
                            "value": "'Page Number' must be greater than '0'."
                          }
                        ]
                    """);
        }
                    

but maybe there is a better way?

Moved scrub xml and scrub json up and us serializer instead
@STRUDSO STRUDSO requested a review from meziantou as a code owner December 16, 2024 06:32
@meziantou
Copy link
Owner

but maybe there is a better way?

You can use a custom converter

var converter = new ScrubDictionaryKeyValuePairValue((key, value) =>
{
    if (key == "traceId")
        return "[redacted]";
    return value;
});
options.AddAttribute<ProblemDetails>(obj => obj.Extensions, new HumanReadableConverterAttribute(converter));
class ScrubDictionaryKeyValuePairValue(Func<string, object?, object?> scrubber) : HumanReadableConverter<IEnumerable<KeyValuePair<string, object?>>>
{
    protected override void WriteValue(HumanReadableTextWriter writer, IEnumerable<KeyValuePair<string, object?>> value, HumanReadableSerializerOptions options)
    {
        Debug.Assert(value is not null);
        if (options.DictionaryKeyOrder is not null)
        {
            value = value.OrderBy(value => value.Key, options.DictionaryKeyOrder);
        }

        var hasItem = false;
        foreach (var prop in value)
        {
            var scrub = scrubber(prop.Key, prop.Value);
            if (scrub is null)
                continue;

            if (!hasItem)
            {
                writer.StartObject();
                hasItem = true;
            }

            writer.WritePropertyName(prop.Key);
            HumanReadableSerializer.Serialize(writer, scrub, options);
        }

        if (hasItem)
        {
            writer.EndObject();
        }
        else
        {
            writer.WriteEmptyObject();
        }
    }
}

@meziantou meziantou merged commit e9e25e9 into meziantou:main Dec 17, 2024
25 checks passed
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