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] += "/";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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/" },
Comment on lines +473 to +474
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ "/cars/{car-id}/build/", ["cars", "{car-id}"], "build/", @"\cars\{car-id}\build/" },
{ "/cars/", [], "cars/", @"\cars/" },
{ "/cars/{car-id}/build/", ["cars", "{car-id}"], @"build\", @"\cars\{car-id}\build\" },
{ "/cars/", [], "cars/", @"\cars\" },

wouldn't this make more sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for you to say.

My interpretation was that \ was used as as a hierarchy separator, and given the last / is not a separator per-se, we might want to keep it as-is to indicate that the last segment is <whatever>/.

If you want the changes in, though, that's fine, I'll push them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as it doesn't derail the logic/node structure, it'd make sense to be consistent.
@darrelmiller I can't remember why backslashes were originally used here instead of forward slashes, would you mind providing some context please?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find out why we used backslash as a separator. It doesn't make sense to me. I tried looking at all the relevant PRs. Maybe @irvinesunday remembers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't used backslash anywhere as a separator within this codebase 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright, thanks for your patience with us @mderriey it seems we suffer from collective amnesia.
In the interest of unblocking this PR, and reducing potential side effects, let's perform the least changes possible, and align everything on backslashes.

};

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