Skip to content

Commit 21d0688

Browse files
committed
Add new properties and remove redundant properties
1 parent 72364c5 commit 21d0688

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.OpenApi.Models;
@@ -18,19 +19,19 @@ public class OpenApiUrlTreeNode
1819
public IDictionary<string, OpenApiUrlTreeNode> Children { get; } = new Dictionary<string, OpenApiUrlTreeNode>();
1920

2021
/// <summary>
21-
/// The name tag for a group of nodes.
22+
/// The relative directory path of the current node from the root node.
2223
/// </summary>
23-
public string Label { get; private set; }
24+
public string Path { get; private set; } = "";
2425

2526
/// <summary>
26-
/// Path Item object that describes the operations available on a node.
27+
/// Dictionary of labels and Path Item objects that describe the operations available on a node.
2728
/// </summary>
28-
public OpenApiPathItem PathItem { get; private set; }
29+
public IDictionary<string, OpenApiPathItem> PathItems { get; private set; }
2930

3031
/// <summary>
31-
/// The relative directory path of the current node from the root node.
32+
/// A container to hold key value pairs of additional data describing a node.
3233
/// </summary>
33-
public string Path { get; private set; } = "";
34+
public IDictionary<string, string> AdditionalData { get; set; }
3435

3536
/// <summary>
3637
/// Flag indicating whether a node segment is a path parameter.
@@ -43,12 +44,17 @@ public class OpenApiUrlTreeNode
4344
public string Segment { get; private set; }
4445

4546
/// <summary>
46-
/// Flag indicating whether a PathItem has operations.
47+
/// Flag indicating whether the node's PathItems has operations.
4748
/// </summary>
4849
/// <returns>true or false.</returns>
49-
public bool HasOperations()
50+
public bool HasOperations(string label)
5051
{
51-
return PathItem?.Operations != null && PathItem.Operations.Count > 0;
52+
if ((bool)!PathItems?.ContainsKey(label))
53+
{
54+
return false;
55+
}
56+
57+
return PathItems[label].Operations != null && PathItems[label].Operations.Count > 0;
5258
}
5359

5460
/// <summary>
@@ -60,18 +66,29 @@ private OpenApiUrlTreeNode(string segment)
6066
Segment = segment;
6167
}
6268

69+
/// <summary>
70+
/// Creates an empty structured directory of <see cref="OpenApiUrlTreeNode"/> node.
71+
/// </summary>
72+
/// <returns>The root node of the created <see cref="OpenApiUrlTreeNode"/> directory structure.</returns>
73+
public static OpenApiUrlTreeNode Create()
74+
{
75+
return new OpenApiUrlTreeNode(string.Empty);
76+
}
77+
6378
/// <summary>
6479
/// Creates a structured directory of <see cref="OpenApiUrlTreeNode"/> nodes from the paths of an OpenAPI document.
6580
/// </summary>
66-
/// <param name="doc">Optional. The OpenAPI document.</param>
67-
/// <param name="label">Optional. Name tag for labelling the <see cref="OpenApiUrlTreeNode"/> nodes
68-
/// in the directory structure.</param>
81+
/// <param name="doc">The OpenAPI document.</param>
82+
/// <param name="label">Name tag for labelling the <see cref="OpenApiUrlTreeNode"/> nodes in the directory structure.</param>
6983
/// <returns>The root node of the created <see cref="OpenApiUrlTreeNode"/> directory structure.</returns>
70-
public static OpenApiUrlTreeNode Create(OpenApiDocument doc = null, string label = "")
84+
public static OpenApiUrlTreeNode Create(OpenApiDocument doc, string label)
7185
{
86+
Utils.CheckArgumentNull(doc, nameof(doc));
87+
Utils.CheckArgumentNullOrEmpty(label, nameof(label));
88+
7289
OpenApiUrlTreeNode root = new OpenApiUrlTreeNode(string.Empty);
7390

74-
var paths = doc?.Paths;
91+
var paths = doc.Paths;
7592
if (paths != null)
7693
{
7794
foreach (var path in paths)
@@ -87,10 +104,12 @@ public static OpenApiUrlTreeNode Create(OpenApiDocument doc = null, string label
87104
/// Retrieves the paths from an OpenAPI document and appends the items to an <see cref="OpenApiUrlTreeNode"/> node.
88105
/// </summary>
89106
/// <param name="doc">The OpenAPI document.</param>
90-
/// <param name="label">Name tag for labelling related <see cref="OpenApiUrlTreeNode"/>
91-
/// nodes in the directory structure.</param>
107+
/// <param name="label">Name tag for labelling related <see cref="OpenApiUrlTreeNode"/> nodes in the directory structure.</param>
92108
public void Attach(OpenApiDocument doc, string label)
93109
{
110+
Utils.CheckArgumentNull(doc, nameof(doc));
111+
Utils.CheckArgumentNullOrEmpty(label, nameof(label));
112+
94113
var paths = doc?.Paths;
95114
if (paths != null)
96115
{
@@ -129,15 +148,22 @@ public OpenApiUrlTreeNode Attach(string path, OpenApiPathItem pathItem, string l
129148
/// <returns>An <see cref="OpenApiUrlTreeNode"/> node with all constituent properties assembled.</returns>
130149
private OpenApiUrlTreeNode Attach(IEnumerable<string> segments, OpenApiPathItem pathItem, string label, string currentPath)
131150
{
151+
if (PathItems.ContainsKey(label))
152+
{
153+
throw new ArgumentException("A duplicate label already exists for this node.", nameof(label));
154+
}
155+
132156
var segment = segments.FirstOrDefault();
133157
if (string.IsNullOrEmpty(segment))
134158
{
135-
if (PathItem == null)
159+
PathItems = new Dictionary<string, OpenApiPathItem>
136160
{
137-
PathItem = pathItem;
138-
Path = currentPath;
139-
Label = label;
140-
}
161+
{
162+
label, pathItem
163+
}
164+
};
165+
Path = currentPath;
166+
141167
return this;
142168
}
143169

0 commit comments

Comments
 (0)