Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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 VerifyDiagramFromSampleOpenAPI()

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