diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs index d3b1949a040..9ee1c28428c 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs @@ -1,4 +1,4 @@ -using Azure.Sdk.Tools.TestProxy.Common; +using Azure.Sdk.Tools.TestProxy.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -65,28 +65,31 @@ public override string SanitizeTextBody(string contentType, string body) } - foreach (JToken token in jsonO.SelectTokens(_jsonPath)) + if (jsonO != null) { - // HasValues is false for tokens with children. We will not apply sanitization if that is the case. - if (!token.HasValues) + foreach (JToken token in jsonO.SelectTokens(_jsonPath)) { - var originalValue = token.Value(); - - // regex replacement does not support null - if (originalValue == null) + // HasValues is false for tokens with children. We will not apply sanitization if that is the case. + if (!token.HasValues) { - continue; - } + var originalValue = token.Value(); - var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace); + // regex replacement does not support null + if (originalValue == null) + { + continue; + } - // this sanitizer should only apply to actual values - // if we attempt to apply a regex update to a jtoken that has a more complex type, throw - token.Replace(JToken.FromObject(replacement)); + var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace); - if(originalValue != replacement) - { - sanitized = true; + // this sanitizer should only apply to actual values + // if we attempt to apply a regex update to a jtoken that has a more complex type, throw + token.Replace(JToken.FromObject(replacement)); + + if (originalValue != replacement) + { + sanitized = true; + } } } }