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() { 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..90e88cc70 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, operationType, 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; } + } } diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index e9d78acb2..a3c284b0e 100755 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1111,7 +1111,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 +1119,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