-
Notifications
You must be signed in to change notification settings - Fork 200
Allows serialization of null valued properties. #3126
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
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7207f8e
Added regex that ensures null valued properties are not omitted
f4df01f
Added JsonExtensions class
21d9976
Updated test to cinclude file from custom folder
d2b9742
Added tests to pipeline
2e95743
Removed conversion of empty strings to null
timayabi2020 cbf6357
Updated submodule
5c427f7
Fixed typo on path
2a7a620
Updated submodule
31df688
Updates submodule
1bf42db
Update submodule
9da3804
Updated submodule
75d4b27
Merge branch 'dev' into enable-null-valued-properties
timayabi2020 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
namespace Microsoft.Graph.PowerShell.JsonUtilities | ||
{ | ||
using Newtonsoft.Json.Linq; | ||
using System.Linq; | ||
|
||
public static class JsonExtensions | ||
{ | ||
/// <summary> | ||
/// Removes JSON properties that have a value of "defaultnull" and converts properties with values of "null" or empty strings ("") to actual JSON null values. | ||
/// </summary> | ||
/// <param name="jsonObject">The JObject to process and clean.</param> | ||
/// <returns> | ||
/// A JSON string representation of the cleaned JObject with "defaultnull" properties removed and "null" or empty string values converted to JSON null. | ||
/// </returns> | ||
/// <example> | ||
/// JObject json = JObject.Parse(@"{""name"": ""John"", ""email"": ""defaultnull"", ""address"": ""null""}"); | ||
/// string cleanedJson = json.RemoveDefaultNullProperties(); | ||
/// Console.WriteLine(cleanedJson); | ||
/// // Output: { "name": "John", "address": null } | ||
/// </example> | ||
public static string RemoveDefaultNullProperties(this JObject jsonObject) | ||
{ | ||
try | ||
{ | ||
foreach (var property in jsonObject.Properties().ToList()) | ||
{ | ||
if (property.Value.Type == JTokenType.Object) | ||
{ | ||
RemoveDefaultNullProperties((JObject)property.Value); | ||
} | ||
else if (property.Value.Type == JTokenType.Array) | ||
{ | ||
foreach (var item in property.Value) | ||
{ | ||
if (item.Type == JTokenType.Object) | ||
{ | ||
RemoveDefaultNullProperties((JObject)item); | ||
} | ||
} | ||
} | ||
else if (property.Value.Type == JTokenType.String && property.Value.ToString() == "defaultnull") | ||
{ | ||
property.Remove(); | ||
} | ||
else if (property.Value.Type == JTokenType.String && (property.Value.ToString() == "null" || property.Value.ToString() == "")) | ||
{ | ||
property.Value = JValue.CreateNull(); | ||
} | ||
} | ||
} | ||
catch (System.Exception) | ||
{ | ||
return jsonObject.ToString(); // Return the original string if parsing fails | ||
} | ||
return jsonObject.ToString(); | ||
} | ||
public static string ReplaceAndRemoveSlashes(this string body) | ||
{ | ||
return body.Replace("/", "").Replace("\\", "").Replace("rn", "").Replace("\"{", "{").Replace("}\"", "}"); | ||
} | ||
} | ||
} |
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,128 @@ | ||
namespace JsonUtilitiesTest; | ||
using System; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
using Microsoft.Graph.PowerShell.JsonUtilities; | ||
|
||
public class JsonExtensionsTests | ||
{ | ||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldRemoveDefaultNullValues() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{ | ||
""displayname"": ""Tim"", | ||
""position"": ""defaultnull"", | ||
""salary"": 2000000, | ||
""team"": ""defaultnull"" | ||
}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.False(result.ContainsKey("position")); | ||
Assert.False(result.ContainsKey("team")); | ||
Assert.Equal("Tim", result["displayname"]?.ToString()); | ||
Assert.Equal(2000000, result["salary"]?.ToObject<int>()); | ||
} | ||
|
||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldConvertStringNullToJsonNull() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{ | ||
""displayname"": ""Tim"", | ||
""position"": ""null"", | ||
""salary"": 2000000, | ||
""team"": """" | ||
}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.Null(result["position"]?.Value<string>()); | ||
Assert.Null(result["team"]?.Value<string>()); | ||
Assert.Equal("Tim", result["displayname"]?.ToString()); | ||
Assert.Equal(2000000, result["salary"]?.ToObject<int>()); | ||
} | ||
|
||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldHandleNestedObjects() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{ | ||
""displayname"": ""Tim"", | ||
""metadata"": { | ||
""phone"": ""defaultnull"", | ||
""location"": ""Nairobi"" | ||
} | ||
}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.False(result["metadata"]?.ToObject<JObject>()?.ContainsKey("phone")); | ||
Assert.Equal("Nairobi", result["metadata"]?["location"]?.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldHandleEmptyJsonObject() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.Empty(result); | ||
} | ||
|
||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldHandleJsonArrays() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{ | ||
""users"": [ | ||
{ ""displayname"": ""Tim"", ""email"": ""defaultnull"" }, | ||
{ ""displayname"": ""Mayabi"", ""email"": ""mayabi@example.com"" } | ||
] | ||
}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.Equal("Tim", result["users"]?[0]?["displayname"]?.ToString()); | ||
Assert.Equal("mayabi@example.com", result["users"]?[1]?["email"]?.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void RemoveDefaultNullProperties_ShouldNotAlterValidData() | ||
{ | ||
// Arrange | ||
JObject json = JObject.Parse(@"{ | ||
""displayname"": ""Tim"", | ||
""email"": ""mayabi@example.com"", | ||
""salary"": 2000000 | ||
}"); | ||
|
||
// Act | ||
string cleanedJson = json.RemoveDefaultNullProperties(); | ||
JObject result = JObject.Parse(cleanedJson); | ||
|
||
// Assert | ||
Assert.Equal("Tim", result["displayname"]?.ToString()); | ||
Assert.Equal("mayabi@example.com", result["email"]?.ToString()); | ||
Assert.Equal(2000000, result["salary"]?.ToObject<int>()); | ||
} | ||
} | ||
|
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
<PackageReference Include="xunit" Version="2.9.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="../../Custom/JsonExtensions.cs"> | ||
<Link>../../Custom/JsonExtensions.cs</Link> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.
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.