Skip to content

Commit aedcfa0

Browse files
Microsoft Graph DevX ToolingMicrosoft Graph DevX Tooling
authored andcommitted
Merge branch 'dev' of https://github.com/microsoftgraph/msgraph-sdk-powershell into fix/nested-json-objs
2 parents f2a809c + 1bb5aa6 commit aedcfa0

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

tools/Tests/JsonUtilitiesTest/JsonExtensionsTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace JsonUtilitiesTest;
22
using System;
3+
using System.Text.Json;
34
using Newtonsoft.Json.Linq;
45
using Xunit;
56
using NamespacePrefixPlaceholder.PowerShell.JsonUtilities;
@@ -161,6 +162,66 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonArray
161162
Assert.Equal("Tim", result[0]?["displayname"]?.ToString());
162163
Assert.False(result[0].ToObject<JObject>().ContainsKey("email"));
163164

165+
}
166+
[Fact]
167+
public void ReplaceAndRemoveSlashes_Should_Preserve_Json_Property_Values()
168+
{
169+
// Arrange
170+
string inputJson = @"{
171+
""RedirectUris"": [""http://localhost/.auth/login/aad/callback""],
172+
""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed""
173+
}";
174+
175+
string expectedJson = @"{
176+
""RedirectUris"": [""http://localhost/.auth/login/aad/callback""],
177+
""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed""
178+
}";
179+
180+
// Act
181+
string result = inputJson.ReplaceAndRemoveSlashes();
182+
183+
// Assert
184+
Assert.Equal(NormalizeJson(expectedJson), NormalizeJson(result));
185+
}
186+
187+
[Fact]
188+
public void ReplaceAndRemoveSlashes_Should_Remove_Backslashes()
189+
{
190+
// Arrange
191+
string input = @"Some \random \slashes that \should be removed.";
192+
string expected = "Some random slashes that should be removed.";
193+
194+
// Act
195+
string result = input.ReplaceAndRemoveSlashes();
196+
197+
// Assert
198+
Assert.Equal(expected, result);
199+
}
200+
201+
[Fact]
202+
public void ReplaceAndRemoveSlashes_Should_Handle_Invalid_Json_Gracefully()
203+
{
204+
// Arrange
205+
string invalidJson = "{Invalid Json \\with /slashes}";
206+
207+
// Act
208+
string result = invalidJson.ReplaceAndRemoveSlashes();
209+
210+
// Assert
211+
Assert.DoesNotContain("\\", result);
212+
}
213+
214+
/// <summary>
215+
/// Normalizes JSON for comparison (removes formatting differences).
216+
/// </summary>
217+
private string NormalizeJson(string json)
218+
{
219+
using var doc = JsonDocument.Parse(json);
220+
return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions
221+
{
222+
WriteIndented = false,
223+
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
224+
});
164225
}
165226

166227
[Fact]

0 commit comments

Comments
 (0)