Skip to content
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

Prevents automatic inference of properties with numeric characters defined as strings. e.g {"age":"28"}" #3190

Merged
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
4 changes: 2 additions & 2 deletions tools/Custom/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static void ProcessBody(JToken token)
{
JToken parsedValue = JToken.Parse(stringValue);
property.Value = parsedValue; // Replace with unescaped JSON object
ProcessBody(parsedValue); // Recursively process
ProcessBody(stringValue); // Recursively process
}
catch (Newtonsoft.Json.JsonException)
{
Expand All @@ -183,7 +183,7 @@ private static void ProcessBody(JToken token)
{
JToken parsedValue = JToken.Parse(stringValue);
jsonArray[i] = parsedValue; // Replace with unescaped JSON object
ProcessBody(parsedValue); // Recursively process
ProcessBody(stringValue); // Recursively process
}
catch (Newtonsoft.Json.JsonException)
{
Expand Down
34 changes: 34 additions & 0 deletions tools/Tests/JsonUtilitiesTest/JsonExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,39 @@ public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValuesInJsonObjec
Assert.False(result["body"]?["users"][0]?.ToObject<JObject>().ContainsKey("email"));
Assert.True(result["body"]?["users"][0]?["metadata"]?.ToObject<JObject>().ContainsKey("phone"));
}

/*
Test for unescaping json object while maintaining original property definition of values
instead of auto inferencing the type of the value
"fields": "{\r\n \"BasicTag\": \"v2.31.0\",\r\n \"BuildID\": \"3599\",\r\n \"MWSCommitID\": \"a5c7998252f2366c8cbbb03ba46e9b\",\r\n \"MWSTag\": \"v2.21.0\",\r\n \"BasicCommitID\": \"9c3d0f36362dd25caa0da2ecab06a1859ce2\",\r\n \"CustomerCommitID\": \"c40241be9fd2f1cd2f2f2fc961c37f720c\"\r\n}"
*/
[Fact]
public void RemoveDefaultNullProperties_ShouldUnescapeJsonString(){
// Arrange
JObject json = JObject.Parse(@"{
""fields"": ""{\r\n \""BasicTag\"": \""v2.31.0\"",\r\n \""BuildID\"": \""3599\"",\r\n \""MWSCommitID\"": \""a5c7998252f2366c8cbbb03ba46e9b\"",\r\n \""MWSTag\"": \""v2.21.0\"",\r\n \""BasicCommitID\"": \""9c3d0f36362dd25caa0da2ecab06a1859ce2\"",\r\n \""CustomerCommitID\"": \""c40241be9fd2f1cd2f2f2fc961c37f720c\""\r\n}""
}");

String expectedJson = @"{
""fields"": {
""BasicTag"": ""v2.31.0"",
""BuildID"": ""3599"",
""MWSCommitID"": ""a5c7998252f2366c8cbbb03ba46e9b"",
""MWSTag"": ""v2.21.0"",
""BasicCommitID"": ""9c3d0f36362dd25caa0da2ecab06a1859ce2"",
""CustomerCommitID"": ""c40241be9fd2f1cd2f2f2fc961c37f720c""
}
}";

// Act
//Convert Json object to string then pass it to RemoveAndReplaceSlashes method
string cleanedJson = json.ToString()?.ReplaceAndRemoveSlashes();

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



}

Loading