Skip to content

Commit

Permalink
fixes #142 by changing includeTags filtering and restoring collection…
Browse files Browse the repository at this point in the history
… snapshots
  • Loading branch information
kirides committed Sep 7, 2023
1 parent 38092e9 commit 6311a60
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/Refitter.Core/RefitGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ private static void ProcessTagFilters(OpenApiDocument document, IReadOnlyCollect
{
return;
}
var clonedPaths = document.Paths.Where(pair => pair.Value != null);
var clonedPaths = document.Paths.Where(pair => pair.Value != null)
// as we modify the document.Paths
// we have to enumerate on a snapshot of the items
.ToArray();
foreach (var path in clonedPaths)
{
var methods = path.Value.Where(pair => pair.Value != null);
var methods = path.Value.Where(pair => pair.Value != null)
// same reason as with document.Paths
.ToArray();
foreach (var method in methods)
{
var exclude = true;
foreach (var tag in includeTags)
{
exclude = method.Value.Tags?.Exists(x => x == tag) != true;
}
var exclude = method.Value.Tags?.Exists(includeTags.Contains) != true;
if (exclude)
{
path.Value.Remove(method.Key);
Expand Down
14 changes: 12 additions & 2 deletions src/Refitter.Tests/Examples/FilterByTagsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public class FilterByTagsTests
responses:
'200':
description: 'successful operation'
/baz:
get:
tags:
- 'Baz'
operationId: 'Get all bazs'
description: 'Get all bazs'
responses:
'200':
description: 'successful operation'
";

[Fact]
Expand All @@ -65,11 +74,12 @@ public async Task Can_Generate_Code()
}

[Fact]
public async Task Generates_Bar_Methods()
public async Task Generates_BarAndBaz_Methods()
{
string generateCode = await GenerateCode();
generateCode.Should().Contain("\"/bar\"");
generateCode.Should().Contain("\"/bar/{id}\"");
generateCode.Should().Contain("\"/baz\"");
generateCode.Should().NotContain("/foo");
}

Expand All @@ -89,7 +99,7 @@ private static async Task<string> GenerateCode()
var settings = new RefitGeneratorSettings
{
OpenApiPath = swaggerFile,
IncludeTags = new[] { "Bar" }
IncludeTags = new[] { "Bar", "Baz" }
};

var sut = await RefitGenerator.CreateAsync(settings);
Expand Down

0 comments on commit 6311a60

Please sign in to comment.