Skip to content

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

@Salatbesteck

Description

@Salatbesteck

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));
 }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions