Skip to content

Commit

Permalink
Merge pull request #1835 from chelkyl/chelkyl/support-trailing-slashes
Browse files Browse the repository at this point in the history
Support trailing slashes in paths
  • Loading branch information
MaggieKimani1 authored Sep 25, 2024
2 parents faa13eb + e18748f commit ff22ab9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ public OpenApiUrlTreeNode Attach(string path,
}

var segments = path.Split('/');
if (path.EndsWith("/", StringComparison.OrdinalIgnoreCase))
{
// Remove the last element, which is empty, and append the trailing slash to the new last element
// This is to support URLs with trailing slashes
Array.Resize(ref segments, segments.Length - 1);
segments[segments.Length - 1] += @"\";
}

return Attach(segments: segments,
pathItem: pathItem,
Expand Down
33 changes: 33 additions & 0 deletions test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,38 @@ public async Task VerifyDiagramFromSampleOpenAPIAsync()

await Verifier.Verify(diagram);
}

public static TheoryData<string, string[], string, string> SupportsTrailingSlashesInPathData => new TheoryData<string, string[], string, string>
{
// Path, children up to second to leaf, last expected leaf node name, expected leaf node path
{ "/cars/{car-id}/build/", ["cars", "{car-id}"], @"build\", @"\cars\{car-id}\build\" },
{ "/cars/", [], @"cars\", @"\cars\" },
};

[Theory]
[MemberData(nameof(SupportsTrailingSlashesInPathData))]
public void SupportsTrailingSlashesInPath(string path, string[] childrenBeforeLastNode, string expectedLeafNodeName, string expectedLeafNodePath)
{
var openApiDocument = new OpenApiDocument
{
Paths = new()
{
[path] = new()
}
};

var label = "trailing-slash";
var rootNode = OpenApiUrlTreeNode.Create(openApiDocument, label);

var secondToLeafNode = rootNode;
foreach (var childName in childrenBeforeLastNode)
{
secondToLeafNode = secondToLeafNode.Children[childName];
}

Assert.True(secondToLeafNode.Children.TryGetValue(expectedLeafNodeName, out var leafNode));
Assert.Equal(expectedLeafNodePath, leafNode.Path);
Assert.Empty(leafNode.Children);
}
}
}

0 comments on commit ff22ab9

Please sign in to comment.