-
Notifications
You must be signed in to change notification settings - Fork 195
Updates implementation of allowing null valued properties to handle array and object schema types differently #3141
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
06cb55a
Updated Json extension class to handle Json Arrays. Updated tests. Up…
8a642ec
Update submodule
26fded2
Updated file based on review feedback
2ec79f3
Rectified error
1afd4c9
Submodule update
0d1b6d5
Handled regex for Microsoft .Graph.Beta.*
026f88d
Testing with failing security module first
4219469
Submodule update
8b87ffc
Made jsonExtension file generic by specifying a namespace place holde…
67e4c56
Submodule update
516a568
Reenabled the other modules
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,85 @@ | ||
namespace Microsoft.Graph.PowerShell.JsonUtilities | ||
namespace NamespacePrefixPlaceholder.PowerShell.JsonUtilities | ||
{ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Linq; | ||
|
||
public static class JsonExtensions | ||
{ | ||
/// <summary> | ||
/// Removes JSON properties that have a value of "defaultnull" and converts properties with values of "null" to actual JSON null values. | ||
/// Recursively removes properties with the value "defaultnull" from a JSON structure | ||
/// and replaces string values that are "null" with actual null values. | ||
/// This method supports both JObject (JSON objects) and JArray (JSON arrays), | ||
/// ensuring proper cleanup of nested structures. | ||
/// </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" values converted to JSON null. | ||
/// </returns> | ||
/// <param name="token">The JToken (JObject or JArray) to process.</param> | ||
/// <returns>The cleaned JSON string with "defaultnull" values removed and "null" strings converted to 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) | ||
public static string RemoveDefaultNullProperties(this JToken token) | ||
{ | ||
try | ||
{ | ||
foreach (var property in jsonObject.Properties().ToList()) | ||
if (token is JObject jsonObject) | ||
{ | ||
if (property.Value.Type == JTokenType.Object) | ||
foreach (var property in jsonObject.Properties().ToList()) | ||
{ | ||
RemoveDefaultNullProperties((JObject)property.Value); | ||
} | ||
else if (property.Value.Type == JTokenType.Array) | ||
{ | ||
foreach (var item in property.Value) | ||
if (property.Value.Type == JTokenType.Object) | ||
{ | ||
if (item.Type == JTokenType.Object) | ||
{ | ||
RemoveDefaultNullProperties((JObject)item); | ||
} | ||
RemoveDefaultNullProperties(property.Value); | ||
} | ||
else if (property.Value.Type == JTokenType.Array) | ||
{ | ||
RemoveDefaultNullProperties(property.Value); | ||
} | ||
else if (property.Value.Type == JTokenType.String && property.Value.ToString().Equals("defaultnull",StringComparison.Ordinal)) | ||
{ | ||
property.Remove(); | ||
} | ||
else if (property.Value.Type == JTokenType.String && property.Value.ToString().Equals("null",StringComparison.Ordinal)) | ||
{ | ||
property.Value = JValue.CreateNull(); | ||
} | ||
} | ||
else if (property.Value.Type == JTokenType.String && property.Value.ToString() == "defaultnull") | ||
{ | ||
property.Remove(); | ||
} | ||
else if (property.Value.Type == JTokenType.String && (property.Value.ToString() == "null")) | ||
} | ||
else if (token is JArray jsonArray) | ||
{ | ||
// Process each item in the JArray | ||
for (int i = jsonArray.Count - 1; i >= 0; i--) | ||
{ | ||
property.Value = JValue.CreateNull(); | ||
var item = jsonArray[i]; | ||
|
||
if (item.Type == JTokenType.Object) | ||
{ | ||
RemoveDefaultNullProperties(item); | ||
} | ||
else if (item.Type == JTokenType.String && item.ToString().Equals("defaultnull",StringComparison.Ordinal)) | ||
{ | ||
jsonArray.RemoveAt(i); // Remove the "defaultnull" string from the array | ||
} | ||
else if (item.Type == JTokenType.String && item.ToString().Equals("null",StringComparison.Ordinal)) | ||
{ | ||
jsonArray[i] = JValue.CreateNull(); // Convert "null" string to actual null | ||
} | ||
} | ||
} | ||
} | ||
catch (System.Exception) | ||
catch (System.Exception ex) | ||
{ | ||
return jsonObject.ToString(); // Return the original string if parsing fails | ||
Console.WriteLine($"Error cleaning JSON: {ex.Message}"); | ||
return token.ToString(); // Return the original JSON if any error occurs | ||
} | ||
return jsonObject.ToString(); | ||
|
||
return token.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
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.