Skip to content

Commit

Permalink
Refactoring Merge two filters into one
Browse files Browse the repository at this point in the history
  • Loading branch information
Koryakov Alexey authored and Koryakov Alexey committed Jan 30, 2024
1 parent a9b2977 commit 21c689a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 90 deletions.
57 changes: 57 additions & 0 deletions src/OnlineSales/Filters/SwaggerEntitiesFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <copyright file="SwaggerEntitiesFilter.cs" company="WavePoint Co. Ltd.">
// Licensed under the MIT license. See LICENSE file in the samples root for full license information.
// </copyright>

using Microsoft.OpenApi.Models;
using Nest;
using OnlineSales.Configuration;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace SwaggerFilters
{
public class SwaggerEntitiesFilter : IDocumentFilter
{
private readonly List<string> includedEntities;
private readonly List<string> excludedEntities;

public SwaggerEntitiesFilter(EntitiesConfig config)
{
includedEntities = excludedEntities = new List<string>();
if (config != null)
{
includedEntities = config.Include.ToList();
excludedEntities = config.Exclude.ToList();
}
}

public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
var schemas = context.SchemaRepository.Schemas;
var excludeSchemas = schemas.Where(s => SchemaNeedsToBeExcluded(s.Key.ToString().ToLower()));
foreach (var schema in excludeSchemas)
{
schemas.Remove(schema.Key);
}

var excludePaths = swaggerDoc.Paths.Where(p => OperationNeedsToBeExcluded(p.Key.ToString()));
foreach (var path in excludePaths)
{
swaggerDoc.Paths.Remove(path.Key);
}
}

public bool SchemaNeedsToBeExcluded(string key)
{
var included = includedEntities.Count == 0 || includedEntities.Exists(s => key.Contains(s.ToLower()));
var excluded = includedEntities.Count == 0 && excludedEntities.Exists(s => key.Contains(s.ToLower()));
return !included || excluded;
}

public bool OperationNeedsToBeExcluded(string path)
{
var included = includedEntities.Count == 0 || includedEntities.Exists(op => path.Contains('/' + op));
var excluded = includedEntities.Count == 0 && excludedEntities.Exists(op => path.Contains('/' + op));
return !included || excluded;
}
}
}
39 changes: 0 additions & 39 deletions src/OnlineSales/Filters/SwaggerExcludeOperationFilter.cs

This file was deleted.

41 changes: 0 additions & 41 deletions src/OnlineSales/Filters/SwaggerExcludeSchemaFilter.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/OnlineSales/OnlineSales.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
<Folder Include="Formatters\Csv\" />
<Folder Include="Elastic\" />
<Folder Include="Elastic\Migrations\" />
<Folder Include="Filters\" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 2 additions & 9 deletions src/OnlineSales/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static void Main(string[] args)
builder.Configuration.AddJsonFile(path, false, true);
});

ConfigureSwaggerFilters(builder);
ConfigureLogs(builder);
PluginManager.Init(builder.Configuration);

Expand Down Expand Up @@ -398,17 +397,11 @@ where p is ISwaggerConfigurator

config.SwaggerDoc("v1", openApiInfo);

config.DocumentFilter<SwaggerExcludeSchemaFilter>();
config.DocumentFilter<SwaggerExcludeOperationFilter>();
var conf = builder.Configuration.GetSection("Entities").Get<EntitiesConfig>();
config.DocumentFilter<SwaggerEntitiesFilter>(conf);
});
}

private static void ConfigureSwaggerFilters(WebApplicationBuilder builder)
{
SwaggerExcludeOperationFilter.Configure(builder.Configuration);
SwaggerExcludeSchemaFilter.Configure(builder.Configuration);
}

private static void ConfigureQuartz(WebApplicationBuilder builder)
{
var taskRunnerSchedule = builder.Configuration.GetValue<string>("TaskRunner:CronSchedule")!;
Expand Down

0 comments on commit 21c689a

Please sign in to comment.