Skip to content

Ensures property values that have / are not modified to have them removed #3153

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/Custom/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static string RemoveDefaultNullProperties(this JToken token)

public static string ReplaceAndRemoveSlashes(this string body)
{
return body.Replace("/", "").Replace("\\", "").Replace("rn", "").Replace("\"{", "{").Replace("}\"", "}");
return body.Replace("\\", "").Replace("rn", "").Replace("\"{", "{").Replace("}\"", "}");
}
}
}
61 changes: 61 additions & 0 deletions tools/Tests/JsonUtilitiesTest/JsonExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace JsonUtilitiesTest;
using System;
using System.Text.Json;
using Newtonsoft.Json.Linq;
using Xunit;
using NamespacePrefixPlaceholder.PowerShell.JsonUtilities;
Expand Down Expand Up @@ -144,6 +145,66 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonArray
Assert.False(result[0].ToObject<JObject>().ContainsKey("email"));

}
[Fact]
public void ReplaceAndRemoveSlashes_Should_Preserve_Json_Property_Values()
{
// Arrange
string inputJson = @"{
""RedirectUris"": [""http://localhost/.auth/login/aad/callback""],
""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed""
}";

string expectedJson = @"{
""RedirectUris"": [""http://localhost/.auth/login/aad/callback""],
""DirectoryPath"": ""/this/is/a/directory/and/should/not/be/removed""
}";

// Act
string result = inputJson.ReplaceAndRemoveSlashes();

// Assert
Assert.Equal(NormalizeJson(expectedJson), NormalizeJson(result));
}

[Fact]
public void ReplaceAndRemoveSlashes_Should_Remove_Backslashes()
{
// Arrange
string input = @"Some \random \slashes that \should be removed.";
string expected = "Some random slashes that should be removed.";

// Act
string result = input.ReplaceAndRemoveSlashes();

// Assert
Assert.Equal(expected, result);
}

[Fact]
public void ReplaceAndRemoveSlashes_Should_Handle_Invalid_Json_Gracefully()
{
// Arrange
string invalidJson = "{Invalid Json \\with /slashes}";

// Act
string result = invalidJson.ReplaceAndRemoveSlashes();

// Assert
Assert.DoesNotContain("\\", result);
}

/// <summary>
/// Normalizes JSON for comparison (removes formatting differences).
/// </summary>
private string NormalizeJson(string json)
{
using var doc = JsonDocument.Parse(json);
return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions
{
WriteIndented = false,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
}


}
Expand Down
Loading