Skip to content

Commit 9dc6ef1

Browse files
Merge pull request #943 from microsoft/mk/fix-missing-path-params
Fixes missing path parameters
2 parents 6a3e948 + 69d0bdd commit 9dc6ef1

File tree

6 files changed

+81
-13
lines changed

6 files changed

+81
-13
lines changed

Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 18 additions & 1 deletion
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;
@@ -135,5 +135,22 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments
135135
var message2 = Assert.Throws<InvalidOperationException>(() => OpenApiFilterService.CreatePredicate("users.user.ListUser", "users.user")).Message;
136136
Assert.Equal("Cannot specify both operationIds and tags at the same time.", message2);
137137
}
138+
139+
[Theory]
140+
[InlineData("reports.getTeamsUserActivityUserDetail-a3f1", null)]
141+
[InlineData(null, "reports.Functions")]
142+
public void ReturnsPathParametersOnSlicingBasedOnOperationIdsOrTags(string operationIds, string tags)
143+
{
144+
// Act
145+
var predicate = OpenApiFilterService.CreatePredicate(operationIds, tags);
146+
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(_openApiDocumentMock, predicate);
147+
148+
// Assert
149+
foreach (var pathItem in subsetOpenApiDocument.Paths)
150+
{
151+
Assert.True(pathItem.Value.Parameters.Any());
152+
Assert.Equal(1, pathItem.Value.Parameters.Count);
153+
}
154+
}
138155
}
139156
}

Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ public static OpenApiDocument CreateOpenApiDocument()
116116
}
117117
}
118118
}
119+
},
120+
Parameters = new List<OpenApiParameter>
121+
{
122+
{
123+
new OpenApiParameter()
124+
{
125+
Name = "period",
126+
In = ParameterLocation.Path,
127+
Required = true,
128+
Schema = new OpenApiSchema()
129+
{
130+
Type = "string"
131+
}
132+
}
133+
}
119134
}
120135
},
121136
["/reports/microsoft.graph.getTeamsUserActivityUserDetail(date={date})"] = new OpenApiPathItem()
@@ -175,7 +190,20 @@ public static OpenApiDocument CreateOpenApiDocument()
175190
}
176191
}
177192
}
178-
}
193+
},
194+
Parameters = new List<OpenApiParameter>
195+
{
196+
new OpenApiParameter
197+
{
198+
Name = "period",
199+
In = ParameterLocation.Path,
200+
Required = true,
201+
Schema = new OpenApiSchema()
202+
{
203+
Type = "string"
204+
}
205+
}
206+
}
179207
},
180208
["/users"] = new OpenApiPathItem()
181209
{

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun
164164
if (result.CurrentKeys.Operation != null)
165165
{
166166
pathItem.Operations.Add((OperationType)result.CurrentKeys.Operation, result.Operation);
167+
168+
if (result.Parameters?.Any() ?? false)
169+
{
170+
foreach (var parameter in result.Parameters)
171+
{
172+
pathItem.Parameters.Add(parameter);
173+
}
174+
}
167175
}
168176
}
169177

src/Microsoft.OpenApi/Services/OperationSearch.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,25 @@ public OperationSearch(Func<string, OperationType?,OpenApiOperation, bool> predi
3131
}
3232

3333
/// <summary>
34-
/// Visits <see cref="OpenApiOperation"/>.
34+
/// Visits <see cref="OpenApiPathItem"/>
3535
/// </summary>
36-
/// <param name="operation">The target <see cref="OpenApiOperation"/>.</param>
37-
public override void Visit(OpenApiOperation operation)
36+
/// <param name="pathItem"> The target <see cref="OpenApiPathItem"/>.</param>
37+
public override void Visit(OpenApiPathItem pathItem)
3838
{
39-
if (_predicate(CurrentKeys.Path, CurrentKeys.Operation, operation))
39+
foreach (var item in pathItem.Operations)
4040
{
41-
_searchResults.Add(new SearchResult()
41+
var operation = item.Value;
42+
var operationType = item.Key;
43+
44+
if (_predicate(CurrentKeys.Path, operationType, operation))
4245
{
43-
Operation = operation,
44-
CurrentKeys = CopyCurrentKeys(CurrentKeys)
45-
});
46+
_searchResults.Add(new SearchResult()
47+
{
48+
Operation = operation,
49+
Parameters = pathItem.Parameters,
50+
CurrentKeys = CopyCurrentKeys(CurrentKeys, operationType)
51+
});
52+
}
4653
}
4754
}
4855

@@ -65,12 +72,12 @@ public override void Visit(IList<OpenApiParameter> parameters)
6572
base.Visit(parameters);
6673
}
6774

68-
private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys)
75+
private static CurrentKeys CopyCurrentKeys(CurrentKeys currentKeys, OperationType operationType)
6976
{
7077
return new CurrentKeys
7178
{
7279
Path = currentKeys.Path,
73-
Operation = currentKeys.Operation
80+
Operation = operationType,
7481
};
7582
}
7683
}

src/Microsoft.OpenApi/Services/SearchResult.cs

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

4+
using System.Collections.Generic;
45
using Microsoft.OpenApi.Models;
56

67
namespace Microsoft.OpenApi.Services
@@ -19,5 +20,11 @@ public class SearchResult
1920
/// An Operation object.
2021
/// </summary>
2122
public OpenApiOperation Operation { get; set; }
23+
24+
/// <summary>
25+
/// Parameters object
26+
/// </summary>
27+
public IList<OpenApiParameter> Parameters { get; set; }
28+
2229
}
2330
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,14 +1111,15 @@ namespace Microsoft.OpenApi.Services
11111111
{
11121112
public OperationSearch(System.Func<string, Microsoft.OpenApi.Models.OperationType?, Microsoft.OpenApi.Models.OpenApiOperation, bool> predicate) { }
11131113
public System.Collections.Generic.IList<Microsoft.OpenApi.Services.SearchResult> SearchResults { get; }
1114-
public override void Visit(Microsoft.OpenApi.Models.OpenApiOperation operation) { }
1114+
public override void Visit(Microsoft.OpenApi.Models.OpenApiPathItem pathItem) { }
11151115
public override void Visit(System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiParameter> parameters) { }
11161116
}
11171117
public class SearchResult
11181118
{
11191119
public SearchResult() { }
11201120
public Microsoft.OpenApi.Services.CurrentKeys CurrentKeys { get; set; }
11211121
public Microsoft.OpenApi.Models.OpenApiOperation Operation { get; set; }
1122+
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiParameter> Parameters { get; set; }
11221123
}
11231124
}
11241125
namespace Microsoft.OpenApi.Validations

0 commit comments

Comments
 (0)