Skip to content

Commit a65d683

Browse files
committed
Update properties and tests
1 parent b462fc5 commit a65d683

File tree

2 files changed

+390
-72
lines changed

2 files changed

+390
-72
lines changed

src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public class OpenApiUrlTreeNode
2626
/// <summary>
2727
/// Dictionary of labels and Path Item objects that describe the operations available on a node.
2828
/// </summary>
29-
public IDictionary<string, OpenApiPathItem> PathItems { get; private set; }
29+
public IDictionary<string, OpenApiPathItem> PathItems { get; } = new Dictionary<string, OpenApiPathItem>();
3030

3131
/// <summary>
32-
/// A container to hold key value pairs of additional data describing a node.
32+
/// A dictionary of key value pairs that contain information about a node.
3333
/// </summary>
34-
public IDictionary<string, string> AdditionalData { get; set; }
34+
public IDictionary<string, List<string>> AdditionalData { get; set; } = new Dictionary<string, List<string>>();
3535

3636
/// <summary>
3737
/// Flag indicating whether a node segment is a path parameter.
@@ -72,7 +72,7 @@ private OpenApiUrlTreeNode(string segment)
7272
/// <returns>The root node of the created <see cref="OpenApiUrlTreeNode"/> directory structure.</returns>
7373
public static OpenApiUrlTreeNode Create()
7474
{
75-
return new OpenApiUrlTreeNode(string.Empty);
75+
return new OpenApiUrlTreeNode("/");
7676
}
7777

7878
/// <summary>
@@ -86,14 +86,16 @@ public static OpenApiUrlTreeNode Create(OpenApiDocument doc, string label)
8686
Utils.CheckArgumentNull(doc, nameof(doc));
8787
Utils.CheckArgumentNullOrEmpty(label, nameof(label));
8888

89-
OpenApiUrlTreeNode root = new OpenApiUrlTreeNode(string.Empty);
89+
OpenApiUrlTreeNode root = new OpenApiUrlTreeNode("/");
9090

9191
var paths = doc.Paths;
9292
if (paths != null)
9393
{
9494
foreach (var path in paths)
9595
{
96-
root.Attach(path.Key, path.Value, label);
96+
root.Attach(path: path.Key,
97+
pathItem: path.Value,
98+
label: label);
9799
}
98100
}
99101

@@ -115,27 +117,38 @@ public void Attach(OpenApiDocument doc, string label)
115117
{
116118
foreach (var path in paths)
117119
{
118-
Attach(path.Key, path.Value, label);
120+
Attach(path: path.Key,
121+
pathItem: path.Value,
122+
label: label);
119123
}
120124
}
121125
}
122126

123127
/// <summary>
124-
/// Appends an OpenAPI path and the PathItems to an <see cref="OpenApiUrlTreeNode"/> node.
128+
/// Appends a path and the PathItem to an <see cref="OpenApiUrlTreeNode"/> node.
125129
/// </summary>
126130
/// <param name="path">An OpenAPI path.</param>
127131
/// <param name="pathItem">Path Item object that describes the operations available on an OpenAPI path.</param>
128132
/// <param name="label">A name tag for labelling the <see cref="OpenApiUrlTreeNode"/> node.</param>
129133
/// <returns>An <see cref="OpenApiUrlTreeNode"/> node describing an OpenAPI path.</returns>
130-
public OpenApiUrlTreeNode Attach(string path, OpenApiPathItem pathItem, string label)
134+
public OpenApiUrlTreeNode Attach(string path,
135+
OpenApiPathItem pathItem,
136+
string label)
131137
{
138+
Utils.CheckArgumentNullOrEmpty(label, nameof(label));
139+
132140
if (path.StartsWith("/"))
133141
{
134142
// Remove leading slash
135143
path = path.Substring(1);
136144
}
145+
137146
var segments = path.Split('/');
138-
return Attach(segments, pathItem, label, "");
147+
148+
return Attach(segments: segments,
149+
pathItem: pathItem,
150+
label: label,
151+
currentPath: "");
139152
}
140153

141154
/// <summary>
@@ -146,31 +159,31 @@ public OpenApiUrlTreeNode Attach(string path, OpenApiPathItem pathItem, string l
146159
/// <param name="label">A name tag for labelling the <see cref="OpenApiUrlTreeNode"/> node.</param>
147160
/// <param name="currentPath">The relative path of a node.</param>
148161
/// <returns>An <see cref="OpenApiUrlTreeNode"/> node with all constituent properties assembled.</returns>
149-
private OpenApiUrlTreeNode Attach(IEnumerable<string> segments, OpenApiPathItem pathItem, string label, string currentPath)
162+
private OpenApiUrlTreeNode Attach(IEnumerable<string> segments,
163+
OpenApiPathItem pathItem,
164+
string label,
165+
string currentPath)
150166
{
151-
if (PathItems.ContainsKey(label))
152-
{
153-
throw new ArgumentException("A duplicate label already exists for this node.", nameof(label));
154-
}
155-
156167
var segment = segments.FirstOrDefault();
157168
if (string.IsNullOrEmpty(segment))
158169
{
159-
PathItems = new Dictionary<string, OpenApiPathItem>
170+
if (PathItems.ContainsKey(label))
160171
{
161-
{
162-
label, pathItem
163-
}
164-
};
165-
Path = currentPath;
172+
throw new ArgumentException("A duplicate label already exists for this node.", nameof(label));
173+
}
166174

175+
Path = currentPath;
176+
PathItems.Add(label, pathItem);
167177
return this;
168178
}
169179

170180
// If the child segment has already been defined, then insert into it
171181
if (Children.ContainsKey(segment))
172182
{
173-
return Children[segment].Attach(segments.Skip(1), pathItem, label, currentPath + "\\" + segment);
183+
return Children[segment].Attach(segments: segments.Skip(1),
184+
pathItem: pathItem,
185+
label: label,
186+
currentPath: currentPath + "\\" + segment);
174187
}
175188
else
176189
{
@@ -180,7 +193,32 @@ private OpenApiUrlTreeNode Attach(IEnumerable<string> segments, OpenApiPathItem
180193
};
181194

182195
Children[segment] = node;
183-
return node.Attach(segments.Skip(1), pathItem, label, currentPath + "\\" + segment);
196+
197+
return node.Attach(segments: segments.Skip(1),
198+
pathItem: pathItem,
199+
label: label,
200+
currentPath: currentPath + "\\" + segment);
201+
}
202+
}
203+
204+
/// <summary>
205+
/// Adds additional data information to the AdditionalData property of the node.
206+
/// </summary>
207+
/// <param name="additionalData">A dictionary of key value pairs that contain information about a node.</param>
208+
public void AddAdditionalData(Dictionary<string, List<string>> additionalData)
209+
{
210+
Utils.CheckArgumentNull(additionalData, nameof(additionalData));
211+
212+
foreach (var item in additionalData)
213+
{
214+
if (AdditionalData.ContainsKey(item.Key))
215+
{
216+
AdditionalData[item.Key] = item.Value;
217+
}
218+
else
219+
{
220+
AdditionalData.Add(item.Key, item.Value);
221+
}
184222
}
185223
}
186224
}

0 commit comments

Comments
 (0)