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 duplication of path parameters during filtering subset doc. #1841

Merged
merged 3 commits into from
Sep 30, 2024
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
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Nullable>enable</Nullable>
<ToolCommandName>hidi</ToolCommandName>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<Version>1.4.9</Version>
<Version>1.4.10</Version>
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.6.21</Version>
<Version>1.6.22</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiFilterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@

if (result.Parameters?.Any() ?? false)
{
foreach (var parameter in result.Parameters)
{
pathItem.Parameters.Add(parameter);
if (!pathItem.Parameters.Contains(parameter))
{
pathItem.Parameters.Add(parameter);
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.
}
}
}
Expand Down
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 Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -105,6 +105,57 @@ public void TestPredicateFiltersUsingRelativeRequestUrls()
Assert.False(predicate("/foo", OperationType.Patch, null));
}

[Fact]
public void CreateFilteredDocumentUsingPredicateFromRequestUrl()
{
// Arrange
var openApiDocument = new OpenApiDocument
{
Info = new() { Title = "Test", Version = "1.0" },
Servers = new List<OpenApiServer> { new() { Url = "https://localhost/" } },
Paths = new()
{
["/test/{id}"] = new()
{
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{ OperationType.Get, new() },
{ OperationType.Patch, new() }
},
Parameters = new List<OpenApiParameter>
{
new()
{
Name = "id",
In = ParameterLocation.Path,
Required = true,
Schema = new()
{
Type = "string"
}
}
}
}


}
};

var requestUrls = new Dictionary<string, List<string>>
{
{"/test/{id}", new List<string> {"GET","PATCH"}}
};

// Act
var predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: openApiDocument);
var subsetDoc = OpenApiFilterService.CreateFilteredDocument(openApiDocument, predicate);

// Assert that there's only 1 parameter in the subset document
Assert.NotNull(subsetDoc);
Assert.NotEmpty(subsetDoc.Paths);
Assert.Single(subsetDoc.Paths.First().Value.Parameters);
}

[Fact]
public void ShouldParseNestedPostmanCollection()
{
Expand Down
Loading