1
- // Copyright (c) Microsoft Corporation. All rights reserved.
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
4
using System . Collections . Generic ;
5
5
using System . Linq ;
6
- using System . Security . Cryptography ;
7
- using System . Text ;
8
- using Microsoft . OpenApi . Extensions ;
9
6
using Microsoft . OpenApi . Models ;
10
7
11
8
namespace Microsoft . OpenApi . Services
12
9
{
13
10
/// <summary>
14
11
/// A directory structure representing the paths of an OpenAPI document.
15
12
/// </summary>
16
- public class OpenApiUrlSpaceNode
13
+ public class OpenApiUrlTreeNode
17
14
{
18
15
/// <summary>
19
16
/// All the subdirectories of a node.
20
17
/// </summary>
21
- public IDictionary < string , OpenApiUrlSpaceNode > Children { get ; } = new Dictionary < string , OpenApiUrlSpaceNode > ( ) ;
18
+ public IDictionary < string , OpenApiUrlTreeNode > Children { get ; } = new Dictionary < string , OpenApiUrlTreeNode > ( ) ;
22
19
23
20
/// <summary>
24
21
/// The name tag for a group of nodes.
@@ -40,41 +37,11 @@ public class OpenApiUrlSpaceNode
40
37
/// </summary>
41
38
public bool IsParameter => Segment . StartsWith ( "{" ) ;
42
39
43
- /// <summary>
44
- /// Flag indicating whether a node segment is a path function.
45
- /// </summary>
46
- public bool IsFunction => Segment . Contains ( "(" ) ;
47
-
48
40
/// <summary>
49
41
/// The subdirectory of a relative path.
50
42
/// </summary>
51
43
public string Segment { get ; private set ; }
52
44
53
- /// <summary>
54
- /// The Pascal-cased alphabet name of a segment.
55
- /// </summary>
56
- public string Identifier
57
- {
58
- get
59
- {
60
- string identifier ;
61
- if ( IsParameter )
62
- {
63
- identifier = Segment . Substring ( 1 , Segment . Length - 2 ) . ToPascalCase ( ) ;
64
- }
65
- else
66
- {
67
- identifier = Segment . ToPascalCase ( ) . Replace ( "()" , "" ) ;
68
- var openParen = identifier . IndexOf ( "(" ) ;
69
- if ( openParen >= 0 )
70
- {
71
- identifier = identifier . Substring ( 0 , openParen ) ;
72
- }
73
- }
74
- return identifier ;
75
- }
76
- }
77
-
78
45
/// <summary>
79
46
/// Flag indicating whether a PathItem has operations.
80
47
/// </summary>
@@ -88,78 +55,39 @@ public bool HasOperations()
88
55
/// Constructor.
89
56
/// </summary>
90
57
/// <param name="segment">The subdirectory of a relative path.</param>
91
- public OpenApiUrlSpaceNode ( string segment )
58
+ private OpenApiUrlTreeNode ( string segment )
92
59
{
93
60
Segment = segment ;
94
61
}
95
62
96
63
/// <summary>
97
- /// Uses SHA256 hash algorithm to hash the Path value.
98
- /// </summary>
99
- /// <returns>The hashed value.</returns>
100
- public string Hash ( )
101
- {
102
- using ( SHA256 sha256Hash = SHA256 . Create ( ) )
103
- {
104
- return GetHash ( sha256Hash , Path ) ;
105
- }
106
- }
107
-
108
- /// <summary>
109
- /// Hashes a string value using a specified hash algorithm.
110
- /// </summary>
111
- /// <param name="hashAlgorithm">The hash algorithm to use for hashing.</param>
112
- /// <param name="input">The string to hash.</param>
113
- /// <returns>The hashed value.</returns>
114
- private static string GetHash ( HashAlgorithm hashAlgorithm , string input )
115
- {
116
- // Convert the input string to a byte array and compute the hash.
117
- byte [ ] data = hashAlgorithm . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
118
-
119
- // Create a new Stringbuilder to collect the bytes
120
- // and create a string.
121
- var sBuilder = new StringBuilder ( ) ;
122
-
123
- // Loop through each byte of the hashed data
124
- // and format each one as a hexadecimal string.
125
- for ( int i = 0 ; i < 2 ; i ++ ) // data.Length Limit to 4 chars
126
- {
127
- sBuilder . Append ( data [ i ] . ToString ( "x2" ) ) ;
128
- }
129
-
130
- // Return the hexadecimal string.
131
- return sBuilder . ToString ( ) ;
132
- }
133
-
134
- /// <summary>
135
- /// Creates a structured directory of <see cref="OpenApiUrlSpaceNode"/> nodes from the paths of an OpenAPI document.
64
+ /// Creates a structured directory of <see cref="OpenApiUrlTreeNode"/> nodes from the paths of an OpenAPI document.
136
65
/// </summary>
137
66
/// <param name="doc">The OpenAPI document.</param>
138
- /// <param name="label">Name tag for labelling the <see cref="OpenApiUrlSpaceNode "/> nodes
67
+ /// <param name="label">Name tag for labelling the <see cref="OpenApiUrlTreeNode "/> nodes
139
68
/// in the directory structure.</param>
140
- /// <returns>The root node of the created <see cref="OpenApiUrlSpaceNode "/> directory structure.</returns>
141
- public static OpenApiUrlSpaceNode Create ( OpenApiDocument doc , string label = "" )
69
+ /// <returns>The root node of the created <see cref="OpenApiUrlTreeNode "/> directory structure.</returns>
70
+ public static OpenApiUrlTreeNode Create ( OpenApiDocument doc , string label = "" )
142
71
{
143
- OpenApiUrlSpaceNode root = null ;
72
+ OpenApiUrlTreeNode root = new OpenApiUrlTreeNode ( string . Empty ) ;
144
73
145
74
var paths = doc ? . Paths ;
146
75
if ( paths != null )
147
76
{
148
- root = new OpenApiUrlSpaceNode ( string . Empty ) ;
149
-
150
77
foreach ( var path in paths )
151
78
{
152
79
root . Attach ( path . Key , path . Value , label ) ;
153
80
}
154
81
}
82
+
155
83
return root ;
156
84
}
157
85
158
86
/// <summary>
159
- /// Retrieves the paths from an OpenAPI document and appends the items to an <see cref="OpenApiUrlSpaceNode "/> node.
87
+ /// Retrieves the paths from an OpenAPI document and appends the items to an <see cref="OpenApiUrlTreeNode "/> node.
160
88
/// </summary>
161
89
/// <param name="doc">The OpenAPI document.</param>
162
- /// <param name="label">Name tag for labelling related <see cref="OpenApiUrlSpaceNode "/>
90
+ /// <param name="label">Name tag for labelling related <see cref="OpenApiUrlTreeNode "/>
163
91
/// nodes in the directory structure.</param>
164
92
public void Attach ( OpenApiDocument doc , string label )
165
93
{
@@ -174,13 +102,13 @@ public void Attach(OpenApiDocument doc, string label)
174
102
}
175
103
176
104
/// <summary>
177
- /// Appends an OpenAPI path and the PathItems to an <see cref="OpenApiUrlSpaceNode "/> node.
105
+ /// Appends an OpenAPI path and the PathItems to an <see cref="OpenApiUrlTreeNode "/> node.
178
106
/// </summary>
179
107
/// <param name="path">An OpenAPI path.</param>
180
108
/// <param name="pathItem">Path Item object that describes the operations available on an OpenAPI path.</param>
181
- /// <param name="label">A name tag for labelling the <see cref="OpenApiUrlSpaceNode "/> node.</param>
182
- /// <returns>An <see cref="OpenApiUrlSpaceNode "/> node describing an OpenAPI path.</returns>
183
- public OpenApiUrlSpaceNode Attach ( string path , OpenApiPathItem pathItem , string label )
109
+ /// <param name="label">A name tag for labelling the <see cref="OpenApiUrlTreeNode "/> node.</param>
110
+ /// <returns>An <see cref="OpenApiUrlTreeNode "/> node describing an OpenAPI path.</returns>
111
+ public OpenApiUrlTreeNode Attach ( string path , OpenApiPathItem pathItem , string label )
184
112
{
185
113
if ( path . StartsWith ( "/" ) )
186
114
{
@@ -192,14 +120,14 @@ public OpenApiUrlSpaceNode Attach(string path, OpenApiPathItem pathItem, string
192
120
}
193
121
194
122
/// <summary>
195
- /// Assembles the constituent properties of an <see cref="OpenApiUrlSpaceNode "/> node.
123
+ /// Assembles the constituent properties of an <see cref="OpenApiUrlTreeNode "/> node.
196
124
/// </summary>
197
125
/// <param name="segments">IEnumerable subdirectories of a relative path.</param>
198
126
/// <param name="pathItem">Path Item object that describes the operations available on an OpenAPI path.</param>
199
- /// <param name="label">A name tag for labelling the <see cref="OpenApiUrlSpaceNode "/> node.</param>
127
+ /// <param name="label">A name tag for labelling the <see cref="OpenApiUrlTreeNode "/> node.</param>
200
128
/// <param name="currentPath">The relative path of a node.</param>
201
- /// <returns>An <see cref="OpenApiUrlSpaceNode "/> node with all constituent properties assembled.</returns>
202
- private OpenApiUrlSpaceNode Attach ( IEnumerable < string > segments , OpenApiPathItem pathItem , string label , string currentPath )
129
+ /// <returns>An <see cref="OpenApiUrlTreeNode "/> node with all constituent properties assembled.</returns>
130
+ private OpenApiUrlTreeNode Attach ( IEnumerable < string > segments , OpenApiPathItem pathItem , string label , string currentPath )
203
131
{
204
132
var segment = segments . FirstOrDefault ( ) ;
205
133
if ( string . IsNullOrEmpty ( segment ) )
@@ -220,7 +148,7 @@ private OpenApiUrlSpaceNode Attach(IEnumerable<string> segments, OpenApiPathItem
220
148
}
221
149
else
222
150
{
223
- var node = new OpenApiUrlSpaceNode ( segment )
151
+ var node = new OpenApiUrlTreeNode ( segment )
224
152
{
225
153
Path = currentPath + "\\ " + segment
226
154
} ;
0 commit comments