1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ using System ;
4
5
using System . Collections . Generic ;
5
6
using System . Linq ;
6
7
using Microsoft . OpenApi . Models ;
@@ -18,19 +19,19 @@ public class OpenApiUrlTreeNode
18
19
public IDictionary < string , OpenApiUrlTreeNode > Children { get ; } = new Dictionary < string , OpenApiUrlTreeNode > ( ) ;
19
20
20
21
/// <summary>
21
- /// The name tag for a group of nodes .
22
+ /// The relative directory path of the current node from the root node .
22
23
/// </summary>
23
- public string Label { get ; private set ; }
24
+ public string Path { get ; private set ; } = "" ;
24
25
25
26
/// <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.
27
28
/// </summary>
28
- public OpenApiPathItem PathItem { get ; private set ; }
29
+ public IDictionary < string , OpenApiPathItem > PathItems { get ; private set ; }
29
30
30
31
/// <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.
32
33
/// </summary>
33
- public string Path { get ; private set ; } = "" ;
34
+ public IDictionary < string , string > AdditionalData { get ; set ; }
34
35
35
36
/// <summary>
36
37
/// Flag indicating whether a node segment is a path parameter.
@@ -43,12 +44,17 @@ public class OpenApiUrlTreeNode
43
44
public string Segment { get ; private set ; }
44
45
45
46
/// <summary>
46
- /// Flag indicating whether a PathItem has operations.
47
+ /// Flag indicating whether the node's PathItems has operations.
47
48
/// </summary>
48
49
/// <returns>true or false.</returns>
49
- public bool HasOperations ( )
50
+ public bool HasOperations ( string label )
50
51
{
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 ;
52
58
}
53
59
54
60
/// <summary>
@@ -60,18 +66,29 @@ private OpenApiUrlTreeNode(string segment)
60
66
Segment = segment ;
61
67
}
62
68
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
+
63
78
/// <summary>
64
79
/// Creates a structured directory of <see cref="OpenApiUrlTreeNode"/> nodes from the paths of an OpenAPI document.
65
80
/// </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>
69
83
/// <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 )
71
85
{
86
+ Utils . CheckArgumentNull ( doc , nameof ( doc ) ) ;
87
+ Utils . CheckArgumentNullOrEmpty ( label , nameof ( label ) ) ;
88
+
72
89
OpenApiUrlTreeNode root = new OpenApiUrlTreeNode ( string . Empty ) ;
73
90
74
- var paths = doc ? . Paths ;
91
+ var paths = doc . Paths ;
75
92
if ( paths != null )
76
93
{
77
94
foreach ( var path in paths )
@@ -87,10 +104,12 @@ public static OpenApiUrlTreeNode Create(OpenApiDocument doc = null, string label
87
104
/// Retrieves the paths from an OpenAPI document and appends the items to an <see cref="OpenApiUrlTreeNode"/> node.
88
105
/// </summary>
89
106
/// <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>
92
108
public void Attach ( OpenApiDocument doc , string label )
93
109
{
110
+ Utils . CheckArgumentNull ( doc , nameof ( doc ) ) ;
111
+ Utils . CheckArgumentNullOrEmpty ( label , nameof ( label ) ) ;
112
+
94
113
var paths = doc ? . Paths ;
95
114
if ( paths != null )
96
115
{
@@ -129,15 +148,22 @@ public OpenApiUrlTreeNode Attach(string path, OpenApiPathItem pathItem, string l
129
148
/// <returns>An <see cref="OpenApiUrlTreeNode"/> node with all constituent properties assembled.</returns>
130
149
private OpenApiUrlTreeNode Attach ( IEnumerable < string > segments , OpenApiPathItem pathItem , string label , string currentPath )
131
150
{
151
+ if ( PathItems . ContainsKey ( label ) )
152
+ {
153
+ throw new ArgumentException ( "A duplicate label already exists for this node." , nameof ( label ) ) ;
154
+ }
155
+
132
156
var segment = segments . FirstOrDefault ( ) ;
133
157
if ( string . IsNullOrEmpty ( segment ) )
134
158
{
135
- if ( PathItem == null )
159
+ PathItems = new Dictionary < string , OpenApiPathItem >
136
160
{
137
- PathItem = pathItem ;
138
- Path = currentPath ;
139
- Label = label ;
140
- }
161
+ {
162
+ label , pathItem
163
+ }
164
+ } ;
165
+ Path = currentPath ;
166
+
141
167
return this ;
142
168
}
143
169
0 commit comments