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

Transformation of routes fails, when duplicate Upstream paths (via wildcard) are specified #320

Open
Salatbesteck opened this issue Dec 10, 2024 · 0 comments
Assignees

Comments

@Salatbesteck
Copy link

When the Ocelot configuration includes both a wildcard route, such as /{everything}, and a dedicated route pointing to the same path, the route transformer fails. Instead of showing the correctly transformed downstream route, it displays the untransformed route.

Expected behaviour
The transformation should be applied and shown in Swagger UI.

To Reproduce
Use the following Ocelot ReRoutes configuration.

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/Version/GetBEVersion",
      "DownstreamScheme": "https",
      "UpstreamPathTemplate": "/mvda/version",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "MVDA",
      "SwaggerKey": "MVDA",
      "Key": "MVDAVersion"
    },
    {
      "UpstreamHttpMethod": [ "GET", "OPTIONS", "POST", "PUT", "DELETE" ],
      "UpstreamPathTemplate": "/mvda/{everything}",
      "DownstreamPathTemplate": "/api/{everything}",
      "DownstreamScheme": "https",
      "AddHeadersToRequest": {
        "Authorization": "{Authorization}"
      },
      "ServiceName": "MVDA",
      "SwaggerKey": "MVDA"
    }
  ]
}

In order to fix this issue the transformer function could be modifed as following:

private void RenameAndRemovePaths(IEnumerable<RouteOptions> routes, JToken paths, string basePath)
 {
     var oldPaths = new List<JProperty>();
     var newPaths = new Dictionary<string, JProperty>(StringComparer.OrdinalIgnoreCase);
     for (int i = 0; i < paths.Count(); i++)
     {
         var oldPath = paths.ElementAt(i) as JProperty;
         oldPaths.Add(oldPath);
         string downstreamPath = oldPath.Name.RemoveSlashFromEnd();
         foreach (var tmpMethod in oldPath.First)
         {
             var method = tmpMethod as JProperty;
             List<RouteOptions> matchedRoutes = FindRoutes(routes, oldPath.Name.WithSlashEnding(), [method.Name](http://method.name/), basePath);
             foreach (var route in matchedRoutes)
             {
                 string newPath = ConvertDownstreamPathToUpstreamPath(
                     downstreamPath, route.DownstreamPath, route.UpstreamPath, basePath);
                 if (!newPaths.TryGetValue(newPath, out JProperty newPathJson))
                 {
                     newPathJson = new JProperty(newPath, new JObject());
                     newPaths.Add(newPath, newPathJson);
                 }
                 var newMethod = (method.DeepClone() as JProperty);
                 AddSecurityDefinition(newMethod, route);
                 if (!((JObject)newPathJson.Value).TryGetValue([newMethod.Name](http://newmethod.name/), out JToken value))
                 {
                     ((JObject)newPathJson.Value).Add(newMethod);
                 }
             }
         }
     }

     oldPaths.ForEach(oldPath => oldPath.Remove());
     newPaths.Select(item => item.Value)
         .OrderBy(newPath => [newPath.Name](http://newpath.name/))
         .ForEach(newPath => ((JObject)paths).Add(newPath));
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants