Skip to content

Commit

Permalink
Added TryParse method to parse json safely
Browse files Browse the repository at this point in the history
  • Loading branch information
rabdulatif committed Oct 6, 2024
1 parent b3d41e6 commit 9486732
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json.Linq;
using System;

namespace MMLib.SwaggerForOcelot.Extensions;

/// <summary>
///
/// </summary>
public static class JsonExtensions
{
/// <summary>
///
/// </summary>
/// <param name="obj"></param>

Check warning on line 14 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

XML comment has a param tag for 'obj', but there is no parameter by that name

Check warning on line 14 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

XML comment has a param tag for 'obj', but there is no parameter by that name
/// <param name="swaggerJson"></param>
/// <returns></returns>
public static bool TryParse(this string swaggerJson, out JObject jObj)

Check warning on line 17 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Parameter 'jObj' has no matching param tag in the XML comment for 'JsonExtensions.TryParse(string, out JObject)' (but other parameters do)

Check warning on line 17 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Parameter 'jObj' has no matching param tag in the XML comment for 'JsonExtensions.TryParse(string, out JObject)' (but other parameters do)
{
try
{
jObj = JObject.Parse(swaggerJson);
return true;
}
catch (Exception ex)

Check warning on line 24 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used

Check warning on line 24 in src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The variable 'ex' is declared but never used
{
jObj = null;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MMLib.SwaggerForOcelot.Configuration;
using MMLib.SwaggerForOcelot.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
Expand Down Expand Up @@ -33,7 +34,9 @@ public string AddServiceNamePrefixToPaths(string swaggerJson, SwaggerEndPointOpt
if (string.IsNullOrEmpty(serviceName))
return swaggerJson;

var swaggerObj = JObject.Parse(swaggerJson);
if (!swaggerJson.TryParse(out var swaggerObj))
return swaggerJson;

if (!swaggerObj.TryGetValue(OpenApiProperties.Paths, out var swaggerPaths))
return swaggerJson;

Expand Down

0 comments on commit 9486732

Please sign in to comment.