Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes missing path parameters #943

Merged
merged 4 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -135,5 +135,22 @@ public void ThrowsInvalidOperationExceptionInCreatePredicateWhenInvalidArguments
var message2 = Assert.Throws<InvalidOperationException>(() => 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);
}
}
}
}
30 changes: 29 additions & 1 deletion Microsoft.OpenApi.Hidi.Tests/UtilityFiles/OpenApiDocumentMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ public static OpenApiDocument CreateOpenApiDocument()
}
}
}
},
Parameters = new List<OpenApiParameter>
{
{
new OpenApiParameter()
{
Name = "period",
In = ParameterLocation.Path,
Required = true,
Schema = new OpenApiSchema()
{
Type = "string"
}
}
}
}
},
["/reports/microsoft.graph.getTeamsUserActivityUserDetail(date={date})"] = new OpenApiPathItem()
Expand Down Expand Up @@ -175,7 +190,20 @@ public static OpenApiDocument CreateOpenApiDocument()
}
}
}
}
},
Parameters = new List<OpenApiParameter>
{
new OpenApiParameter
{
Name = "period",
In = ParameterLocation.Path,
Required = true,
Schema = new OpenApiSchema()
{
Type = "string"
}
}
}
},
["/users"] = new OpenApiPathItem()
{
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.OpenApi/Services/OpenApiFilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

Expand Down
27 changes: 17 additions & 10 deletions src/Microsoft.OpenApi/Services/OperationSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ public OperationSearch(Func<string, OperationType?,OpenApiOperation, bool> predi
}

/// <summary>
/// Visits <see cref="OpenApiOperation"/>.
/// Visits <see cref="OpenApiPathItem"/>
/// </summary>
/// <param name="operation">The target <see cref="OpenApiOperation"/>.</param>
public override void Visit(OpenApiOperation operation)
/// <param name="pathItem"> The target <see cref="OpenApiPathItem"/>.</param>
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)
});
}
}
}

Expand All @@ -65,12 +72,12 @@ public override void Visit(IList<OpenApiParameter> 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,
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Microsoft.OpenApi/Services/SearchResult.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,5 +20,11 @@ public class SearchResult
/// An Operation object.
/// </summary>
public OpenApiOperation Operation { get; set; }

/// <summary>
/// Parameters object
/// </summary>
public IList<OpenApiParameter> Parameters { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1111,14 +1111,15 @@ namespace Microsoft.OpenApi.Services
{
public OperationSearch(System.Func<string, Microsoft.OpenApi.Models.OperationType?, Microsoft.OpenApi.Models.OpenApiOperation, bool> predicate) { }
public System.Collections.Generic.IList<Microsoft.OpenApi.Services.SearchResult> 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<Microsoft.OpenApi.Models.OpenApiParameter> parameters) { }
}
public class SearchResult
{
public SearchResult() { }
public Microsoft.OpenApi.Services.CurrentKeys CurrentKeys { get; set; }
public Microsoft.OpenApi.Models.OpenApiOperation Operation { get; set; }
public System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiParameter> Parameters { get; set; }
}
}
namespace Microsoft.OpenApi.Validations
Expand Down