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
19 changes: 10 additions & 9 deletions src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
{
Utils.CheckArgumentNullOrEmpty(label);

return PathItems is not null && PathItems.TryGetValue(label, out var item) && item.Operations is not null && item.Operations.Any();
return PathItems is not null && PathItems.TryGetValue(label, out var item) && item.Operations is { Count : > 0 };
}

/// <summary>
Expand Down Expand Up @@ -93,7 +93,7 @@

var root = Create();

var paths = doc?.Paths;

Check warning on line 96 in src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. (https://rules.sonarsource.com/csharp/RSPEC-2589)

Check warning on line 96 in src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unnecessary check for null. (https://rules.sonarsource.com/csharp/RSPEC-2589)
if (paths != null)
{
foreach (var path in paths)
Expand Down Expand Up @@ -151,13 +151,6 @@
}

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 All @@ -179,7 +172,8 @@
string currentPath)
{
var segment = segments.FirstOrDefault();
if (string.IsNullOrEmpty(segment))
// empty segment is significant
if (segment is null)
{
if (PathItems.ContainsKey(label))
{
Expand All @@ -190,6 +184,13 @@
PathItems.Add(label, pathItem);
return this;
}
// special casing for '/' (root node)
if (segment.Length == 0 && currentPath.Length == 0)
{
Path = currentPath;
PathItems.Add(label, pathItem);
return this;
}

// If the child segment has already been defined, then insert into it
if (Children.TryGetValue(segment, out var child))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,66 @@ public void CreatePathsWithMultipleSegmentsWorks()
Assert.Equal("coupes", rootNode.Children["cars"].Children["coupes"].Segment);
}

[Fact]
public void CreatePathsWithTrailingSlashWorks()
{
var doc = new OpenApiDocument
{
Paths = new()
{
["/"] = new OpenApiPathItem(),
["/cars"] = new OpenApiPathItem(),
["/cars/"] = new OpenApiPathItem(),
["/cars/coupes"] = new OpenApiPathItem()
}
};

var label = "assets";
var rootNode = OpenApiUrlTreeNode.Create(doc, label);

Assert.NotNull(rootNode);
Assert.Single(rootNode.Children);
var carsNode = rootNode.Children["cars"];
Assert.Equal("cars", carsNode.Segment);
Assert.Equal(2, carsNode.Children.Count);
var emptyNode = carsNode.Children[""];
Assert.Empty(emptyNode.Segment);
Assert.Empty(emptyNode.Children);
var coupesNode = carsNode.Children["coupes"];
Assert.Equal("coupes", coupesNode.Segment);
Assert.Empty(coupesNode.Children);
}

[Fact]
public void CreatePathsWithTrailingSlashWorksInverted()
{
var doc = new OpenApiDocument
{
Paths = new()
{
["/"] = new OpenApiPathItem(),
["/cars/"] = new OpenApiPathItem(),
["/cars"] = new OpenApiPathItem(), // only difference from previous test, tests that node mapping is not order specific
["/cars/coupes"] = new OpenApiPathItem()
}
};

var label = "assets";
var rootNode = OpenApiUrlTreeNode.Create(doc, label);

Assert.NotNull(rootNode);
Assert.Single(rootNode.Children);
var carsNode = rootNode.Children["cars"];
Assert.Equal("cars", carsNode.Segment);
Assert.Equal(2, carsNode.Children.Count);
var emptyNode = carsNode.Children[""];
Assert.Empty(emptyNode.Segment);
Assert.Empty(emptyNode.Children);
var coupesNode = carsNode.Children["coupes"];
Assert.Equal("coupes", coupesNode.Segment);
Assert.Empty(coupesNode.Children);
}

[Fact]
public void HasOperationsWorks()
{
Expand Down Expand Up @@ -468,16 +528,16 @@ public async Task VerifyDiagramFromSampleOpenAPIAsync()
await Verifier.Verify(diagram);
}

public static TheoryData<string, string[], string, string> SupportsTrailingSlashesInPathData => new TheoryData<string, string[], string, string>
public static TheoryData<string, string[], string> SupportsTrailingSlashesInPathData => new TheoryData<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\" },
{ "/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)
public void SupportsTrailingSlashesInPath(string path, string[] childrenBeforeLastNode, string expectedLeafNodePath)
{
var openApiDocument = new OpenApiDocument
{
Expand All @@ -496,7 +556,7 @@ public void SupportsTrailingSlashesInPath(string path, string[] childrenBeforeLa
secondToLeafNode = secondToLeafNode.Children[childName];
}

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