-
Notifications
You must be signed in to change notification settings - Fork 235
Compare json object values instead of byte streams when matching bodies that are content-type json
#8860
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
Merged
Merged
Compare json object values instead of byte streams when matching bodies that are content-type json
#8860
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4347902
first cut, time to test
scbedd 2e33645
now compiling. two failing matcher tests
scbedd 59377d3
tests repaired
scbedd 5ab2b1c
clean up usings
scbedd 719b270
change the ordering so we aren't ALWAYS expanding the full json
scbedd aaef1d4
ensure we normalize so that the json comparison is the fallback
scbedd eda681e
protect json comparer from invalid json
scbedd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Azure.Sdk.Tools.TestProxy.Common | ||
| { | ||
| public class JsonComparer | ||
| { | ||
| public static List<string> CompareJson(byte[] json1, byte[] json2) | ||
| { | ||
| // Deserialize the byte arrays to JsonDocument | ||
| JsonDocument doc1 = JsonDocument.Parse(json1); | ||
| JsonDocument doc2 = JsonDocument.Parse(json2); | ||
|
|
||
| // Compare the JSON objects | ||
| var differences = new List<string>(); | ||
| CompareElements(doc1.RootElement, doc2.RootElement, differences, ""); | ||
|
|
||
| return differences; | ||
| } | ||
|
|
||
| private static void CompareElements(JsonElement element1, JsonElement element2, List<string> differences, string path) | ||
| { | ||
| if (element1.ValueKind != element2.ValueKind) | ||
| { | ||
| differences.Add($"{path}: Request and record have different types."); | ||
| return; | ||
| } | ||
|
|
||
| switch (element1.ValueKind) | ||
| { | ||
| case JsonValueKind.Object: | ||
| { | ||
| var properties1 = element1.EnumerateObject(); | ||
| var properties2 = element2.EnumerateObject(); | ||
|
|
||
| var propDict1 = new Dictionary<string, JsonElement>(); | ||
| var propDict2 = new Dictionary<string, JsonElement>(); | ||
|
|
||
| foreach (var prop in properties1) | ||
| propDict1[prop.Name] = prop.Value; | ||
|
|
||
| foreach (var prop in properties2) | ||
| propDict2[prop.Name] = prop.Value; | ||
|
|
||
| foreach (var key in propDict1.Keys) | ||
| { | ||
| if (propDict2.ContainsKey(key)) | ||
| { | ||
| CompareElements(propDict1[key], propDict2[key], differences, $"{path}.{key}"); | ||
| } | ||
| else | ||
| { | ||
| differences.Add($"{path}.{key}: Missing in request JSON"); | ||
| } | ||
| } | ||
|
|
||
| foreach (var key in propDict2.Keys) | ||
| { | ||
| if (!propDict1.ContainsKey(key)) | ||
| { | ||
| differences.Add($"{path}.{key}: Missing in record JSON"); | ||
| } | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| case JsonValueKind.Array: | ||
| { | ||
| var array1 = element1.EnumerateArray(); | ||
| var array2 = element2.EnumerateArray(); | ||
|
|
||
| int index = 0; | ||
| var enum1 = array1.GetEnumerator(); | ||
| var enum2 = array2.GetEnumerator(); | ||
|
|
||
| while (enum1.MoveNext() && enum2.MoveNext()) | ||
| { | ||
| CompareElements(enum1.Current, enum2.Current, differences, $"{path}[{index}]"); | ||
| index++; | ||
| } | ||
|
|
||
| while (enum1.MoveNext()) | ||
| { | ||
| differences.Add($"{path}[{index}]: Extra element in request JSON"); | ||
| index++; | ||
| } | ||
|
|
||
| while (enum2.MoveNext()) | ||
| { | ||
| differences.Add($"{path}[{index}]: Extra element in record JSON"); | ||
| index++; | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| case JsonValueKind.String: | ||
| { | ||
| if (element1.GetString() != element2.GetString()) | ||
| { | ||
| differences.Add($"{path}: \"{element1.GetString()}\" != \"{element2.GetString()}\""); | ||
| } | ||
| break; | ||
| } | ||
| case JsonValueKind.Number: | ||
| { | ||
| if (element1.GetDecimal() != element2.GetDecimal()) | ||
| { | ||
| differences.Add($"{path}: {element1.GetDecimal()} != {element2.GetDecimal()}"); | ||
| } | ||
| break; | ||
| } | ||
| case JsonValueKind.True: | ||
| case JsonValueKind.False: | ||
| { | ||
| if (element1.GetBoolean() != element2.GetBoolean()) | ||
| { | ||
| differences.Add($"{path}: {element1.GetBoolean()} != {element2.GetBoolean()}"); | ||
| } | ||
| break; | ||
| } | ||
| case JsonValueKind.Null: | ||
| { | ||
| // Both are null, nothing to compare | ||
| break; | ||
| } | ||
| default: | ||
| { | ||
| differences.Add($"{path}: Unhandled value kind {element1.ValueKind}"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.