Skip to content

Commit 3016370

Browse files
committed
Change class name; remove unnecessary methods and properties
1 parent a82a451 commit 3016370

File tree

2 files changed

+34
-127
lines changed

2 files changed

+34
-127
lines changed

src/Microsoft.OpenApi/Services/OpenApiUrlSpaceNode.cs renamed to src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs

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

44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Security.Cryptography;
7-
using System.Text;
8-
using Microsoft.OpenApi.Extensions;
96
using Microsoft.OpenApi.Models;
107

118
namespace Microsoft.OpenApi.Services
129
{
1310
/// <summary>
1411
/// A directory structure representing the paths of an OpenAPI document.
1512
/// </summary>
16-
public class OpenApiUrlSpaceNode
13+
public class OpenApiUrlTreeNode
1714
{
1815
/// <summary>
1916
/// All the subdirectories of a node.
2017
/// </summary>
21-
public IDictionary<string, OpenApiUrlSpaceNode> Children { get; } = new Dictionary<string, OpenApiUrlSpaceNode>();
18+
public IDictionary<string, OpenApiUrlTreeNode> Children { get; } = new Dictionary<string, OpenApiUrlTreeNode>();
2219

2320
/// <summary>
2421
/// The name tag for a group of nodes.
@@ -40,41 +37,11 @@ public class OpenApiUrlSpaceNode
4037
/// </summary>
4138
public bool IsParameter => Segment.StartsWith("{");
4239

43-
/// <summary>
44-
/// Flag indicating whether a node segment is a path function.
45-
/// </summary>
46-
public bool IsFunction => Segment.Contains("(");
47-
4840
/// <summary>
4941
/// The subdirectory of a relative path.
5042
/// </summary>
5143
public string Segment { get; private set; }
5244

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-
7845
/// <summary>
7946
/// Flag indicating whether a PathItem has operations.
8047
/// </summary>
@@ -88,78 +55,39 @@ public bool HasOperations()
8855
/// Constructor.
8956
/// </summary>
9057
/// <param name="segment">The subdirectory of a relative path.</param>
91-
public OpenApiUrlSpaceNode(string segment)
58+
private OpenApiUrlTreeNode(string segment)
9259
{
9360
Segment = segment;
9461
}
9562

9663
/// <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.
13665
/// </summary>
13766
/// <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
13968
/// 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 = "")
14271
{
143-
OpenApiUrlSpaceNode root = null;
72+
OpenApiUrlTreeNode root = new OpenApiUrlTreeNode(string.Empty);
14473

14574
var paths = doc?.Paths;
14675
if (paths != null)
14776
{
148-
root = new OpenApiUrlSpaceNode(string.Empty);
149-
15077
foreach (var path in paths)
15178
{
15279
root.Attach(path.Key, path.Value, label);
15380
}
15481
}
82+
15583
return root;
15684
}
15785

15886
/// <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.
16088
/// </summary>
16189
/// <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"/>
16391
/// nodes in the directory structure.</param>
16492
public void Attach(OpenApiDocument doc, string label)
16593
{
@@ -174,13 +102,13 @@ public void Attach(OpenApiDocument doc, string label)
174102
}
175103

176104
/// <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.
178106
/// </summary>
179107
/// <param name="path">An OpenAPI path.</param>
180108
/// <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)
184112
{
185113
if (path.StartsWith("/"))
186114
{
@@ -192,14 +120,14 @@ public OpenApiUrlSpaceNode Attach(string path, OpenApiPathItem pathItem, string
192120
}
193121

194122
/// <summary>
195-
/// Assembles the constituent properties of an <see cref="OpenApiUrlSpaceNode"/> node.
123+
/// Assembles the constituent properties of an <see cref="OpenApiUrlTreeNode"/> node.
196124
/// </summary>
197125
/// <param name="segments">IEnumerable subdirectories of a relative path.</param>
198126
/// <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>
200128
/// <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)
203131
{
204132
var segment = segments.FirstOrDefault();
205133
if (string.IsNullOrEmpty(segment))
@@ -220,7 +148,7 @@ private OpenApiUrlSpaceNode Attach(IEnumerable<string> segments, OpenApiPathItem
220148
}
221149
else
222150
{
223-
var node = new OpenApiUrlSpaceNode(segment)
151+
var node = new OpenApiUrlTreeNode(segment)
224152
{
225153
Path = currentPath + "\\" + segment
226154
};

test/Microsoft.OpenApi.Tests/Services/OpenApiUrlSpaceTests.cs renamed to test/Microsoft.OpenApi.Tests/Services/OpenApiUrlTreeNodeTests.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
namespace Microsoft.OpenApi.Tests.Services
1010
{
11-
public class OpenApiUrlSpaceTests
11+
public class OpenApiUrlTreeNodeTests
1212
{
1313
[Fact]
14-
public void CreateEmptyUrlSpace()
14+
public void CreateEmptyUrlTreeNode()
1515
{
1616
var doc = new OpenApiDocument() { };
1717

18-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
18+
var rootNode = OpenApiUrlTreeNode.Create(doc);
19+
var rootNode1 = OpenApiUrlTreeNode.Create(null);
1920

20-
Assert.Null(rootNode);
21+
Assert.NotNull(rootNode);
22+
Assert.NotNull(rootNode1);
2123
}
2224

2325
[Fact]
@@ -31,7 +33,7 @@ public void CreateSingleRootWorks()
3133
}
3234
};
3335

34-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
36+
var rootNode = OpenApiUrlTreeNode.Create(doc);
3537

3638
Assert.NotNull(rootNode);
3739
Assert.NotNull(rootNode.PathItem);
@@ -49,7 +51,7 @@ public void CreatePathWithoutRootWorks()
4951
}
5052
};
5153

52-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
54+
var rootNode = OpenApiUrlTreeNode.Create(doc);
5355

5456
Assert.NotNull(rootNode);
5557
Assert.Null(rootNode.PathItem);
@@ -71,7 +73,7 @@ public void CreateMultiplePathsWorks()
7173
}
7274
};
7375

74-
var rootNode = OpenApiUrlSpaceNode.Create(doc, "Ensuite");
76+
var rootNode = OpenApiUrlTreeNode.Create(doc, "Ensuite");
7577

7678
Assert.NotNull(rootNode);
7779
Assert.Equal(2, rootNode.Children.Count);
@@ -105,7 +107,7 @@ public void AttachDocumentWorks()
105107
}
106108
};
107109

108-
var rootNode = OpenApiUrlSpaceNode.Create(doc1, "Penthouse");
110+
var rootNode = OpenApiUrlTreeNode.Create(doc1, "Penthouse");
109111
rootNode.Attach(doc2, "Five-star");
110112

111113
Assert.NotNull(rootNode);
@@ -127,7 +129,7 @@ public void CreatePathsWithMultipleSegmentsWorks()
127129
}
128130
};
129131

130-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
132+
var rootNode = OpenApiUrlTreeNode.Create(doc);
131133

132134
Assert.NotNull(rootNode);
133135
Assert.Equal(2, rootNode.Children.Count);
@@ -168,34 +170,12 @@ public void HasOperationsWorks()
168170
}
169171
};
170172

171-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
173+
var rootNode = OpenApiUrlTreeNode.Create(doc);
172174
Assert.NotNull(rootNode);
173175
Assert.False(rootNode.Children["home"].HasOperations());
174176
Assert.True(rootNode.Children["car"].HasOperations());
175177
}
176178

177-
[Fact]
178-
public void SegmentIsFunctionWorks()
179-
{
180-
var doc = new OpenApiDocument()
181-
{
182-
Paths = new OpenApiPaths()
183-
{
184-
["/"] = new OpenApiPathItem(),
185-
["/home/gate/open()"] = new OpenApiPathItem()
186-
}
187-
};
188-
189-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
190-
191-
Assert.NotNull(rootNode);
192-
Assert.Equal(1, rootNode.Children.Count);
193-
Assert.NotNull(rootNode.Children["home"].Children["gate"].Children["open()"].PathItem);
194-
Assert.True(rootNode.Children["home"].Children["gate"].Children["open()"].IsFunction);
195-
Assert.Equal("Open", rootNode.Children["home"].Children["gate"].Children["open()"].Identifier);
196-
Assert.Equal("open()", rootNode.Children["home"].Children["gate"].Children["open()"].Segment);
197-
}
198-
199179
[Fact]
200180
public void SegmentIsParameterWorks()
201181
{
@@ -208,13 +188,12 @@ public void SegmentIsParameterWorks()
208188
}
209189
};
210190

211-
var rootNode = OpenApiUrlSpaceNode.Create(doc);
191+
var rootNode = OpenApiUrlTreeNode.Create(doc);
212192

213193
Assert.NotNull(rootNode);
214194
Assert.Equal(1, rootNode.Children.Count);
215195
Assert.NotNull(rootNode.Children["home"].Children["bedroom"].Children["{bedroom-id}"].PathItem);
216196
Assert.True(rootNode.Children["home"].Children["bedroom"].Children["{bedroom-id}"].IsParameter);
217-
Assert.Equal("BedroomId", rootNode.Children["home"].Children["bedroom"].Children["{bedroom-id}"].Identifier);
218197
Assert.Equal("{bedroom-id}", rootNode.Children["home"].Children["bedroom"].Children["{bedroom-id}"].Segment);
219198
}
220199
}

0 commit comments

Comments
 (0)