Skip to content

Commit eda681e

Browse files
committed
protect json comparer from invalid json
1 parent aaef1d4 commit eda681e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Text;
25
using System.Text.Json;
36

47
namespace Azure.Sdk.Tools.TestProxy.Common
@@ -7,12 +10,32 @@ public class JsonComparer
710
{
811
public static List<string> CompareJson(byte[] json1, byte[] json2)
912
{
13+
var differences = new List<string>();
14+
JsonDocument doc1;
15+
JsonDocument doc2;
16+
1017
// Deserialize the byte arrays to JsonDocument
11-
JsonDocument doc1 = JsonDocument.Parse(json1);
12-
JsonDocument doc2 = JsonDocument.Parse(json2);
18+
try
19+
{
20+
doc1 = JsonDocument.Parse(json1);
21+
}
22+
catch(Exception ex)
23+
{
24+
differences.Add($"Unable to parse the request json body. Content \"{Encoding.UTF8.GetString(json1)}.\" Exception: {ex.Message}");
25+
return differences;
26+
}
27+
28+
try
29+
{
30+
doc2 = JsonDocument.Parse(json2);
31+
}
32+
33+
catch (Exception ex)
34+
{
35+
differences.Add($"Unable to parse the record json body. Content \"{Encoding.UTF8.GetString(json2)}.\" Exception: {ex.Message}");
36+
return differences;
37+
}
1338

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

1841
return differences;

0 commit comments

Comments
 (0)