From b39303e235fdd42bb9c54859e8c1409304585e44 Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Thu, 14 Jul 2022 16:17:25 +0300 Subject: [PATCH 1/4] Retrieve missing path parameters during slicing --- .../Services/OpenApiFilterService.cs | 8 ++++++ .../Services/OperationSearch.cs | 27 ++++++++++++------- .../Services/SearchResult.cs | 7 +++++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.OpenApi/Services/OpenApiFilterService.cs b/src/Microsoft.OpenApi/Services/OpenApiFilterService.cs index 11dcaec14..7b9df3d0e 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiFilterService.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiFilterService.cs @@ -164,6 +164,14 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun if (result.CurrentKeys.Operation != null) { pathItem.Operations.Add((OperationType)result.CurrentKeys.Operation, result.Operation); + + if (result.Parameters?.Any() ?? false) + { + foreach (var parameter in result.Parameters) + { + pathItem.Parameters.Add(parameter); + } + } } } diff --git a/src/Microsoft.OpenApi/Services/OperationSearch.cs b/src/Microsoft.OpenApi/Services/OperationSearch.cs index 95b3a6341..780c7ebd8 100644 --- a/src/Microsoft.OpenApi/Services/OperationSearch.cs +++ b/src/Microsoft.OpenApi/Services/OperationSearch.cs @@ -31,18 +31,25 @@ public OperationSearch(Func predi } /// - /// Visits . + /// Visits /// - /// The target . - public override void Visit(OpenApiOperation operation) + /// The target . + public override void Visit(OpenApiPathItem pathItem) { - if (_predicate(CurrentKeys.Path, CurrentKeys.Operation, operation)) + foreach (var item in pathItem.Operations) { - _searchResults.Add(new SearchResult() + var operation = item.Value; + var operationType = item.Key; + + if (_predicate(CurrentKeys.Path, CurrentKeys.Operation, operation)) { - Operation = operation, - CurrentKeys = CopyCurrentKeys(CurrentKeys) - }); + _searchResults.Add(new SearchResult() + { + Operation = operation, + Parameters = pathItem.Parameters, + CurrentKeys = CopyCurrentKeys(CurrentKeys, operationType) + }); + } } } @@ -65,12 +72,12 @@ public override void Visit(IList parameters) base.Visit(parameters); } - private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys) + private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys, OperationType operationType) { return new CurrentKeys { Path = currentKeys.Path, - Operation = currentKeys.Operation + Operation = operationType }; } } diff --git a/src/Microsoft.OpenApi/Services/SearchResult.cs b/src/Microsoft.OpenApi/Services/SearchResult.cs index 381a11f95..435711128 100644 --- a/src/Microsoft.OpenApi/Services/SearchResult.cs +++ b/src/Microsoft.OpenApi/Services/SearchResult.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System.Collections.Generic; using Microsoft.OpenApi.Models; namespace Microsoft.OpenApi.Services @@ -19,5 +20,11 @@ public class SearchResult /// An Operation object. /// public OpenApiOperation Operation { get; set; } + + /// + /// Parameters object + /// + public IList Parameters { get; set; } + } } From ea2d73f5bb04775e377a54233f3ff8f6ae33853d Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Thu, 14 Jul 2022 16:59:15 +0300 Subject: [PATCH 2/4] Adds test for retrieving path parameters --- .../Services/OpenApiFilterServiceTests.cs | 19 +++++++++++- .../UtilityFiles/OpenApiDocumentMock.cs | 30 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs b/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs index 29cb684d2..176fb20d1 100644 --- a/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs +++ b/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System; @@ -135,5 +135,22 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments var message2 = Assert.Throws(() => OpenApiFilterService.CreatePredicate("users.user.ListUser", "users.user")).Message; Assert.Equal("Cannot specify both operationIds and tags at the same time.", message2); } + + [Theory] + [InlineData("reports.getTeamsUserActivityUserDetail-a3f1", null)] + [InlineData(null, "reports.Functions")] + public void ReturnsPathParametersOnSlicingBasedOnOperationIdsOrTags(string operationIds, string tags) + { + // Act + var predicate = OpenApiFilterService.CreatePredicate(operationIds, tags); + var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate); + + // Assert + foreach (var pathItem in subsetOpenApiDocument.Paths) + { + Assert.True(pathItem.Value.Parameters.Any()); + Assert.Equal(1, pathItem.Value.Parameters.Count); + } + } } } diff --git a/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs b/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs index d21fccb9a..58b85d91d 100644 --- a/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs +++ b/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs @@ -116,6 +116,21 @@ public static OpenApiDocument CreateOpenApiDocument() } } } + }, + Parameters = new List + { + { + new OpenApiParameter() + { + Name = "period", + In = ParameterLocation.Path, + Required = true, + Schema = new OpenApiSchema() + { + Type = "string" + } + } + } } }, ["/reports/microsoft.graph.getTeamsUserActivityUserDetail(date={date})"] = new OpenApiPathItem() @@ -175,7 +190,20 @@ public static OpenApiDocument CreateOpenApiDocument() } } } - } + }, + Parameters = new List + { + new OpenApiParameter + { + Name = "period", + In = ParameterLocation.Path, + Required = true, + Schema = new OpenApiSchema() + { + Type = "string" + } + } + } }, ["/users"] = new OpenApiPathItem() { From 51d5018b2eac078362ac836e0dc27bbec0b4a62d Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Fri, 15 Jul 2022 15:58:19 +0300 Subject: [PATCH 3/4] Code refactor and update public API interface --- src/Microsoft.OpenApi/Services/OperationSearch.cs | 4 ++-- test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.OpenApi/Services/OperationSearch.cs b/src/Microsoft.OpenApi/Services/OperationSearch.cs index 780c7ebd8..90e88cc70 100644 --- a/src/Microsoft.OpenApi/Services/OperationSearch.cs +++ b/src/Microsoft.OpenApi/Services/OperationSearch.cs @@ -41,7 +41,7 @@ public override void Visit(OpenApiPathItem pathItem) var operation = item.Value; var operationType = item.Key; - if (_predicate(CurrentKeys.Path, CurrentKeys.Operation, operation)) + if (_predicate(CurrentKeys.Path, operationType, operation)) { _searchResults.Add(new SearchResult() { @@ -77,7 +77,7 @@ private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys, OperationTyp return new CurrentKeys { Path = currentKeys.Path, - Operation = operationType + Operation = operationType, }; } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index e9d78acb2..e7dc531c7 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1006,6 +1006,7 @@ namespace Microsoft.OpenApi.Services public string Header { get; } public string Link { get; set; } public Microsoft.OpenApi.Models.OperationType? Operation { get; set; } + public System.Collections.Generic.IList Parameters { get; set; } public string Path { get; set; } public string Response { get; set; } public string ServerVariable { get; } @@ -1111,7 +1112,7 @@ namespace Microsoft.OpenApi.Services { public OperationSearch(System.Func predicate) { } public System.Collections.Generic.IList SearchResults { get; } - public override void Visit(Microsoft.OpenApi.Models.OpenApiOperation operation) { } + public override void Visit(Microsoft.OpenApi.Models.OpenApiPathItem pathItem) { } public override void Visit(System.Collections.Generic.IList parameters) { } } public class SearchResult @@ -1119,6 +1120,7 @@ namespace Microsoft.OpenApi.Services public SearchResult() { } public Microsoft.OpenApi.Services.CurrentKeys CurrentKeys { get; set; } public Microsoft.OpenApi.Models.OpenApiOperation Operation { get; set; } + public System.Collections.Generic.IList Parameters { get; set; } } } namespace Microsoft.OpenApi.Validations From 69d0bdd667f04a82fdb8a8b7bf1a2c3975eafead Mon Sep 17 00:00:00 2001 From: Maggie Kimani Date: Fri, 15 Jul 2022 16:25:42 +0300 Subject: [PATCH 4/4] Revert API change --- test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index e7dc531c7..a3c284b0e 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1006,7 +1006,6 @@ namespace Microsoft.OpenApi.Services public string Header { get; } public string Link { get; set; } public Microsoft.OpenApi.Models.OperationType? Operation { get; set; } - public System.Collections.Generic.IList Parameters { get; set; } public string Path { get; set; } public string Response { get; set; } public string ServerVariable { get; }