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

Add service name prefixing logic to Swagger paths in SwaggerForOcelotMiddleware #311

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
content = _transformer.Transform(content, routeOptions, GetServerName(context, endPoint), endPoint);
}
}
else
{
content = _transformer.AddServiceNamePrefixToPaths(content, endPoint, version);

}

Check notice on line 108 in src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs

View check run for this annotation

codefactor.io / CodeFactor

src/MMLib.SwaggerForOcelot/Middleware/SwaggerForOcelotMiddleware.cs#L108

A closing brace should not be preceded by a blank line. (SA1508)
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved

content = await ReconfigureUpstreamSwagger(context, content);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ string Transform(string swaggerJson,
IEnumerable<RouteOptions> routes,
string serverOverride,
SwaggerEndPointOptions endPointOptions);

/// <summary>
/// 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.
/// </summary>
/// <param name="swaggerJson">The original Swagger JSON as a string.</param>
/// <param name="serviceName">The service name to be prefixed to each path in the Swagger JSON.</param>
/// <param name="version"></param>
/// <returns>
/// 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.
/// </returns>
string AddServiceNamePrefixToPaths(string swaggerJson, SwaggerEndPointOptions serviceName, string version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using MMLib.SwaggerForOcelot.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MMLib.SwaggerForOcelot.Transformation;

/// <summary>
///
/// </summary>
public partial class SwaggerJsonTransformer
{
/// <summary>
/// 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.
/// </summary>
/// <param name="swaggerJson">The original Swagger JSON as a string.</param>
/// <param name="serviceName">The service name to be prefixed to each path in the Swagger JSON.</param>
/// <param name="version"></param>
/// <returns>
/// 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.
/// </returns>
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved
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;

var swaggerObj = JObject.Parse(swaggerJson);
if (!swaggerObj.TryGetValue(OpenApiProperties.Paths, out var swaggerPaths))
return swaggerJson;
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved

if (swaggerPaths is not JObject pathsObj)
return swaggerJson;

var properties = pathsObj.Properties().ToList();
properties.ForEach(f => SetToPathServiceName(f, pathsObj, serviceName));
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved

return swaggerObj.ToString();
}

/// <summary>
///
/// </summary>
/// <param name="jProperty"></param>
/// <param name="pathsObj"></param>
/// <param name="serviceName"></param>
private void SetToPathServiceName(JProperty jProperty, JObject pathsObj, string serviceName)
{
jProperty.Remove();

var path = $"/{serviceName}{jProperty.Name}";
pathsObj.Add(path, jProperty.Value);
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved
}
rabdulatif marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace MMLib.SwaggerForOcelot.Transformation
/// Class which implement transformation downstream service swagger json into upstream format
/// </summary>
/// <seealso cref="ISwaggerJsonTransformer" />
public class SwaggerJsonTransformer : ISwaggerJsonTransformer
public partial class SwaggerJsonTransformer : ISwaggerJsonTransformer
{
private readonly OcelotSwaggerGenOptions _ocelotSwaggerGenOptions;
private readonly IMemoryCache _memoryCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public string Transform(string swaggerJson,
{
return _transformedJson;
}

public string AddServiceNamePrefixToPaths(string swaggerJson,
SwaggerEndPointOptions serviceName,
string version) => _transformedJson;
}
}
}
Loading