Skip to content

Commit 35398a1

Browse files
committed
Attempt at supporting trailing slashes
1 parent ad775b2 commit 35398a1

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ public OpenApiUrlTreeNode Attach(string path,
150150
}
151151

152152
var segments = path.Split('/');
153+
if (path.EndsWith("/"))
154+
{
155+
// Remove the last element, which is empty, and append the trailing slash to the new last element
156+
// This is to support URLs with trailing slashes
157+
Array.Resize(ref segments, segments.Length - 1);
158+
segments[segments.Length - 1] += "/";
159+
}
153160

154161
return Attach(segments: segments,
155162
pathItem: pathItem,

test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,22 +467,36 @@ public async Task VerifyDiagramFromSampleOpenAPI()
467467
await Verifier.Verify(diagram);
468468
}
469469

470-
[Fact]
471-
public void SupportsTrailingSlashesInPath()
470+
public static TheoryData<string, string[], string, string> SupportsTrailingSlashesInPathData => new TheoryData<string, string[], string, string>
471+
{
472+
// Path, children up to second to leaf, last expected leaf node name, expected leaf node path
473+
{ "/cars/{car-id}/build/", ["cars", "{car-id}"], "build/", @"\cars\{car-id}\build/" },
474+
{ "/cars/", [], "cars/", @"\cars/" },
475+
};
476+
477+
[Theory]
478+
[MemberData(nameof(SupportsTrailingSlashesInPathData))]
479+
public void SupportsTrailingSlashesInPath(string path, string[] childrenBeforeLastNode, string expectedLeafNodeName, string expectedLeafNodePath)
472480
{
473481
var openApiDocument = new OpenApiDocument
474482
{
475483
Paths = new()
476484
{
477-
["/cars/{car-id}/build/"] = new()
485+
[path] = new()
478486
}
479487
};
480488

481-
var label1 = "trailing-slash";
482-
var rootNode = OpenApiUrlTreeNode.Create(openApiDocument, label1);
483-
var buildNode = rootNode.Children["cars"].Children["{car-id}"].Children["build"];
489+
var label = "trailing-slash";
490+
var rootNode = OpenApiUrlTreeNode.Create(openApiDocument, label);
491+
492+
var secondToLeafNode = rootNode;
493+
foreach (var childName in childrenBeforeLastNode)
494+
{
495+
secondToLeafNode = secondToLeafNode.Children[childName];
496+
}
484497

485-
// Should buildNode have a path of "build/" or should it have a child with an empty string key?
498+
Assert.True(secondToLeafNode.Children.TryGetValue(expectedLeafNodeName, out var lastNode));
499+
Assert.Equal(expectedLeafNodePath, lastNode.Path);
486500
}
487501
}
488502
}

0 commit comments

Comments
 (0)