diff --git a/src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs b/src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs new file mode 100644 index 0000000..f56c288 --- /dev/null +++ b/src/MMLib.SwaggerForOcelot/Extensions/JsonExtensions.cs @@ -0,0 +1,30 @@ +using Newtonsoft.Json.Linq; +using System; + +namespace MMLib.SwaggerForOcelot.Extensions; + +/// +/// +/// +public static class JsonExtensions +{ + /// + /// + /// + /// + /// + /// + public static bool TryParse(this string swaggerJson, out JObject jObj) + { + try + { + jObj = JObject.Parse(swaggerJson); + return true; + } + catch (Exception ex) + { + jObj = null; + return false; + } + } +} diff --git a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs index d15c279..095208f 100644 --- a/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs +++ b/src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs @@ -101,6 +101,11 @@ public async Task Invoke(HttpContext context, content = _transformer.Transform(content, routeOptions, GetServerName(context, endPoint), endPoint); } } + else + { + content = _transformer.AddServiceNamePrefixToPaths(content, endPoint, version); + + } content = await ReconfigureUpstreamSwagger(context, content); diff --git a/src/MMLib.SwaggerForOcelot/Transformation/ISwaggerJsonTransformer.cs b/src/MMLib.SwaggerForOcelot/Transformation/ISwaggerJsonTransformer.cs index be875d0..00d4dc7 100644 --- a/src/MMLib.SwaggerForOcelot/Transformation/ISwaggerJsonTransformer.cs +++ b/src/MMLib.SwaggerForOcelot/Transformation/ISwaggerJsonTransformer.cs @@ -22,5 +22,18 @@ string Transform(string swaggerJson, IEnumerable routes, string serverOverride, SwaggerEndPointOptions endPointOptions); + + /// + /// Modifies the paths in a given Swagger JSON by adding a specified service name as a prefix to each path. + /// If the "paths" section is missing or null, the method returns the original Swagger JSON without modifications. + /// + /// The original Swagger JSON as a string. + /// The service name to be prefixed to each path in the Swagger JSON. + /// + /// + /// A modified Swagger JSON string where each path in the "paths" section is prefixed with the provided service name. + /// If the "paths" section does not exist or is null, the original Swagger JSON is returned. + /// + string AddServiceNamePrefixToPaths(string swaggerJson, SwaggerEndPointOptions serviceName, string version); } } diff --git a/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.Consul.cs b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.Consul.cs new file mode 100644 index 0000000..d7533ba --- /dev/null +++ b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.Consul.cs @@ -0,0 +1,65 @@ +using MMLib.SwaggerForOcelot.Configuration; +using MMLib.SwaggerForOcelot.Extensions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace MMLib.SwaggerForOcelot.Transformation; + +/// +/// +/// +public partial class SwaggerJsonTransformer +{ + /// + /// Modifies the paths in a given Swagger JSON by adding a specified service name as a prefix to each path. + /// If the "paths" section is missing or null, the method returns the original Swagger JSON without modifications. + /// + /// The original Swagger JSON as a string. + /// The service name to be prefixed to each path in the Swagger JSON. + /// + /// + /// A modified Swagger JSON string where each path in the "paths" section is prefixed with the provided service name. + /// If the "paths" section does not exist or is null, the original Swagger JSON is returned. + /// + public string AddServiceNamePrefixToPaths(string swaggerJson, SwaggerEndPointOptions endPoint, string version) + { + var config = string.IsNullOrEmpty(version) + ? endPoint.Config.FirstOrDefault() + : endPoint.Config.FirstOrDefault(x => x.Version == version); + + var serviceName = config?.Service?.Name; + if (string.IsNullOrEmpty(serviceName)) + return swaggerJson; + + if (!swaggerJson.TryParse(out var swaggerObj)) + return swaggerJson; + + if (!swaggerObj.TryGetValue(OpenApiProperties.Paths, out var swaggerPaths)) + return swaggerJson; + + if (swaggerPaths is not JObject pathsObj) + return swaggerJson; + + var properties = pathsObj.Properties().ToList(); + properties.ForEach(f => SetToPathServiceName(f, pathsObj, serviceName)); + + return swaggerObj.ToString(); + } + + /// + /// + /// + /// + /// + /// + private void SetToPathServiceName(JProperty jProperty, JObject pathsObj, string serviceName) + { + jProperty.Remove(); + + var path = $"/{serviceName}{jProperty.Name}"; + pathsObj.Add(path, jProperty.Value); + } +} diff --git a/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs index c4db880..9033b44 100644 --- a/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs +++ b/src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs @@ -15,7 +15,7 @@ namespace MMLib.SwaggerForOcelot.Transformation /// Class which implement transformation downstream service swagger json into upstream format /// /// - public class SwaggerJsonTransformer : ISwaggerJsonTransformer + public partial class SwaggerJsonTransformer : ISwaggerJsonTransformer { private readonly OcelotSwaggerGenOptions _ocelotSwaggerGenOptions; private readonly IMemoryCache _memoryCache; diff --git a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs index a8b4d33..a1c2018 100644 --- a/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs +++ b/tests/MMLib.SwaggerForOcelot.Tests/SwaggerForOcelotMiddlewareShould.cs @@ -494,6 +494,10 @@ public string Transform(string swaggerJson, { return _transformedJson; } + + public string AddServiceNamePrefixToPaths(string swaggerJson, + SwaggerEndPointOptions serviceName, + string version) => _transformedJson; } } }