Skip to content

Commit af25fe4

Browse files
committed
Add recursive solution for nested collection item object
1 parent 194576e commit af25fe4

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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;
@@ -301,24 +301,41 @@ public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream st
301301
logger.LogTrace("Parsing the json collection file into a JsonDocument");
302302
using var document = JsonDocument.Parse(stream);
303303
var root = document.RootElement;
304-
var itemElement = root.GetProperty("item");
305-
foreach (var requestObject in itemElement.EnumerateArray().Select(item => item.GetProperty("request")))
306-
{
307-
// Fetch list of methods and urls from collection, store them in a dictionary
308-
var path = requestObject.GetProperty("url").GetProperty("raw").ToString();
309-
var method = requestObject.GetProperty("method").ToString();
310304

311-
if (!requestUrls.ContainsKey(path))
305+
requestUrls = Enumerate(root, requestUrls);
306+
307+
logger.LogTrace("Finished fetching the list of paths and Http methods defined in the Postman collection.");
308+
return requestUrls;
309+
}
310+
311+
private static Dictionary<string, List<string>> Enumerate(JsonElement itemElement, Dictionary<string, List<string>> paths)
312+
{
313+
var itemsArray = itemElement.GetProperty("item");
314+
315+
foreach (var item in itemsArray.EnumerateArray())
316+
{
317+
if (item.TryGetProperty("request", out var request))
312318
{
313-
requestUrls.Add(path, new List<string> { method });
319+
// Fetch list of methods and urls from collection, store them in a dictionary
320+
var path = request.GetProperty("url").GetProperty("raw").ToString();
321+
var method = request.GetProperty("method").ToString();
322+
323+
if (!paths.ContainsKey(path))
324+
{
325+
paths.Add(path, new List<string> { method });
326+
}
327+
else
328+
{
329+
paths[path].Add(method);
330+
}
314331
}
315332
else
316333
{
317-
requestUrls[path].Add(method);
334+
Enumerate(item, paths);
318335
}
319336
}
320-
logger.LogTrace("Finished fetching the list of paths and Http methods defined in the Postman collection.");
321-
return requestUrls;
337+
338+
return paths;
322339
}
323340

324341
internal static async Task<int> ValidateOpenApiDocument(string openapi, LogLevel loglevel, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)