Is there a way to scrub (inline) Guids from JSON properties?

282 views Asked by At

My code generates a mapping in JSON where the property names are inline Guids. When verifying the output it always generates a new result, because the Guids in the property names are not scrubbed.

[Test]
public Task GuidIsScrubbed()
{
    const string serialized = @"{
        ""GuidAsValue"": ""ee7f4fa8-48b4-48b7-b962-586870a09d4e"",
            ""EmbeddedGuidAsValue"": ""Something(ee7f4fa8-48b4-48b7-b962-586870a09d4e)Something"",
            ""ee7f4fa8-48b4-48b7-b962-586870a09d4e"": ""GuidAsKey"",
            ""Something(ee7f4fa8-48b4-48b7-b962-586870a09d4e)Something"": ""EmbeddedGuidAsKey""
        }";

    var verifySettings = new VerifySettings();
    verifySettings.ScrubInlineGuids();
    return Verifier.VerifyJson(serialized, verifySettings);
}

generates the following output

{
  GuidAsValue: Guid_1,
  EmbeddedGuidAsValue: Something(Guid_1)Something,
  ee7f4fa8-48b4-48b7-b962-586870a09d4e: GuidAsKey,
  Something(ee7f4fa8-48b4-48b7-b962-586870a09d4e)Something: EmbeddedGuidAsKey
}

Adding my own very simple scrubber verifySettings.ScrubLinesWithReplace(x => x.Replace("ee7f4fa8-48b4-48b7-b962-586870a09d4e", "MyGuid")) also only replaces the Guids in the values, so it does not seem to be a problem with the built-in Guid scrubber, but rather a setting that I am missing.

Is there a way to scrub the Guids from the property names directly with VerifyTests? For now I have scrubbed all Guids before passing it to the verifier.

2

There are 2 answers

0
starfruit3254 On

I had the same problem and ended up using this:

VerifierSettings.MemberConverter<Foo, string>(
expression: foo=>foo.Id, 
converter: (target, member) => "String_Guid");

see: https://github.com/VerifyTests/Verify/blob/main/docs/serializer-settings.md#converting-a-member

0
Simon On

currently, no, this is not supported