Skip to content

Commit e035981

Browse files
authored
Merge pull request #1841 from microsoft/is/filtered-doc-fix
Fixes duplication of path parameters during filtering subset doc.
2 parents 776d584 + c2d3f99 commit e035981

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Nullable>enable</Nullable>
1010
<ToolCommandName>hidi</ToolCommandName>
1111
<PackageOutputPath>./../../artifacts</PackageOutputPath>
12-
<Version>1.4.9</Version>
12+
<Version>1.4.10</Version>
1313
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
1414
<SignAssembly>true</SignAssembly>
1515
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>Latest</LangVersion>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.6.21</Version>
6+
<Version>1.6.22</Version>
77
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
88
<SignAssembly>true</SignAssembly>
99
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ public static OpenApiDocument CreateFilteredDocument(OpenApiDocument source, Fun
112112
{
113113
foreach (var parameter in result.Parameters)
114114
{
115-
pathItem.Parameters.Add(parameter);
115+
if (!pathItem.Parameters.Contains(parameter))
116+
{
117+
pathItem.Parameters.Add(parameter);
118+
}
116119
}
117120
}
118121
}

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

Lines changed: 52 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 Microsoft.Extensions.Logging;
@@ -105,6 +105,57 @@ public void TestPredicateFiltersUsingRelativeRequestUrls()
105105
Assert.False(predicate("/foo", OperationType.Patch, null));
106106
}
107107

108+
[Fact]
109+
public void CreateFilteredDocumentUsingPredicateFromRequestUrl()
110+
{
111+
// Arrange
112+
var openApiDocument = new OpenApiDocument
113+
{
114+
Info = new() { Title = "Test", Version = "1.0" },
115+
Servers = new List<OpenApiServer> { new() { Url = "https://localhost/" } },
116+
Paths = new()
117+
{
118+
["/test/{id}"] = new()
119+
{
120+
Operations = new Dictionary<OperationType, OpenApiOperation>
121+
{
122+
{ OperationType.Get, new() },
123+
{ OperationType.Patch, new() }
124+
},
125+
Parameters = new List<OpenApiParameter>
126+
{
127+
new()
128+
{
129+
Name = "id",
130+
In = ParameterLocation.Path,
131+
Required = true,
132+
Schema = new()
133+
{
134+
Type = "string"
135+
}
136+
}
137+
}
138+
}
139+
140+
141+
}
142+
};
143+
144+
var requestUrls = new Dictionary<string, List<string>>
145+
{
146+
{"/test/{id}", new List<string> {"GET","PATCH"}}
147+
};
148+
149+
// Act
150+
var predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: openApiDocument);
151+
var subsetDoc = OpenApiFilterService.CreateFilteredDocument(openApiDocument, predicate);
152+
153+
// Assert that there's only 1 parameter in the subset document
154+
Assert.NotNull(subsetDoc);
155+
Assert.NotEmpty(subsetDoc.Paths);
156+
Assert.Single(subsetDoc.Paths.First().Value.Parameters);
157+
}
158+
108159
[Fact]
109160
public void ShouldParseNestedPostmanCollection()
110161
{

0 commit comments

Comments
 (0)