Skip to content

Commit

Permalink
protect json comparer from invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Aug 23, 2024
1 parent aaef1d4 commit eda681e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;

namespace Azure.Sdk.Tools.TestProxy.Common
Expand All @@ -7,12 +10,32 @@ public class JsonComparer
{
public static List<string> CompareJson(byte[] json1, byte[] json2)
{
var differences = new List<string>();
JsonDocument doc1;
JsonDocument doc2;

// Deserialize the byte arrays to JsonDocument
JsonDocument doc1 = JsonDocument.Parse(json1);
JsonDocument doc2 = JsonDocument.Parse(json2);
try
{
doc1 = JsonDocument.Parse(json1);
}
catch(Exception ex)
{
differences.Add($"Unable to parse the request json body. Content \"{Encoding.UTF8.GetString(json1)}.\" Exception: {ex.Message}");
return differences;
}

try
{
doc2 = JsonDocument.Parse(json2);
}

catch (Exception ex)
{
differences.Add($"Unable to parse the record json body. Content \"{Encoding.UTF8.GetString(json2)}.\" Exception: {ex.Message}");
return differences;
}

// Compare the JSON objects
var differences = new List<string>();
CompareElements(doc1.RootElement, doc2.RootElement, differences, "");

return differences;
Expand Down

0 comments on commit eda681e

Please sign in to comment.