Skip to content

Commit 685543c

Browse files
Ticket #902 : Can exclude endpoints from swagger
1 parent a7fe136 commit 685543c

4 files changed

Lines changed: 97 additions & 13 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace SimpleIdServer.IdServer.Swagger.Filters;
5+
6+
public class ExcludeEndpointsConfig
7+
{
8+
public string[] SegmentsToExclude { get; set; } = Array.Empty<string>();
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) SimpleIdServer. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
using Microsoft.OpenApi.Models;
4+
using Swashbuckle.AspNetCore.SwaggerGen;
5+
6+
namespace SimpleIdServer.IdServer.Swagger.Filters;
7+
8+
public class ExcludeEndpointsDocumentFilter : IDocumentFilter
9+
{
10+
private readonly ExcludeEndpointsConfig _config;
11+
12+
public ExcludeEndpointsDocumentFilter(ExcludeEndpointsConfig config)
13+
{
14+
_config = config;
15+
}
16+
17+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
18+
{
19+
if (_config.SegmentsToExclude == null || _config.SegmentsToExclude.Length == 0)
20+
{
21+
return;
22+
}
23+
24+
var pathsToRemove = swaggerDoc.Paths
25+
.Where(path =>
26+
_config.SegmentsToExclude.Any(segment =>
27+
path.Key.TrimStart('/')
28+
.StartsWith(segment.Trim('/'), StringComparison.OrdinalIgnoreCase))
29+
)
30+
.Select(p => p.Key)
31+
.ToList();
32+
33+
foreach (var path in pathsToRemove)
34+
{
35+
swaggerDoc.Paths.Remove(path);
36+
}
37+
}
38+
}

src/IdServer/SimpleIdServer.IdServer.Swagger/IdServerBuilderExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ namespace Microsoft.Extensions.DependencyInjection;
1111

1212
public static class IdServerBuilderExtensions
1313
{
14-
public static IdServerBuilder AddSwagger(this IdServerBuilder builder, Action<IdServerSwaggerApiConfiguration> action = null)
14+
public static IdServerBuilder AddSwagger(
15+
this IdServerBuilder builder,
16+
Action<IdServerSwaggerApiConfiguration> action = null)
1517
{
18+
var conf = new IdServerSwaggerApiConfiguration(builder.Services);
19+
if (action != null)
20+
{
21+
action(conf);
22+
}
23+
1624
builder.Services.AddSwaggerGen(o =>
1725
{
18-
o.SchemaFilter<DescribeEnumMemberValues>();
19-
var conf = new IdServerSwaggerApiConfiguration(o);
26+
conf.Options = o;
27+
conf.Configure();
2028
conf.AddOAuthSecurity();
21-
if(action != null)
22-
{
23-
action(conf);
24-
}
29+
o.SchemaFilter<DescribeEnumMemberValues>();
2530
});
2631
builder.Services.RemoveAll<IApiDescriptionGroupCollectionProvider>();
2732
builder.Services.AddSingleton<IApiDescriptionGroupCollectionProvider, SidApiDescriptionGroupCollectionProvider>();

src/IdServer/SimpleIdServer.IdServer.Swagger/IdServerSwaggerApiConfiguration.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,63 @@
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.OpenApi.Models;
5+
using SimpleIdServer.IdServer.Swagger.Filters;
56
using Swashbuckle.AspNetCore.SwaggerGen;
67

78
namespace SimpleIdServer.IdServer.Swagger;
89

910
public class IdServerSwaggerApiConfiguration
1011
{
11-
private readonly SwaggerGenOptions _options;
12+
private readonly IServiceCollection _services;
13+
private readonly List<string> _xmlPaths;
14+
private bool _addDocumentFilter;
1215

13-
internal IdServerSwaggerApiConfiguration(SwaggerGenOptions options)
16+
internal IdServerSwaggerApiConfiguration(IServiceCollection services)
1417
{
15-
_options = options;
18+
_services = services;
19+
_xmlPaths = new List<string>();
20+
}
21+
22+
internal SwaggerGenOptions Options
23+
{
24+
get; set;
1625
}
1726

1827
public IdServerSwaggerApiConfiguration IncludeDocumentation<T>()
1928
{
2029
var assm = typeof(T).Assembly.ManifestModule.Name.Replace(".dll", string.Empty);
2130
var xmlFile = $"{assm}.xml";
2231
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
23-
_options.IncludeXmlComments(xmlPath);
32+
_xmlPaths.Add(xmlPath);
2433
return this;
2534
}
2635

36+
public IdServerSwaggerApiConfiguration ExcludeDocumentations(params string[] filters)
37+
{
38+
_services.AddSingleton(new ExcludeEndpointsConfig
39+
{
40+
SegmentsToExclude = filters.ToArray()
41+
});
42+
_addDocumentFilter = true;
43+
return this;
44+
}
45+
46+
internal void Configure()
47+
{
48+
foreach(var xmlPath in _xmlPaths)
49+
{
50+
Options.IncludeXmlComments(xmlPath);
51+
}
52+
53+
if(_addDocumentFilter)
54+
{
55+
Options.DocumentFilter<ExcludeEndpointsDocumentFilter>();
56+
}
57+
}
58+
2759
internal IdServerSwaggerApiConfiguration AddOAuthSecurity()
2860
{
29-
_options.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
61+
Options.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
3062
{
3163
Type = SecuritySchemeType.OAuth2,
3264
Flows = new OpenApiOAuthFlows
@@ -58,7 +90,7 @@ internal IdServerSwaggerApiConfiguration AddOAuthSecurity()
5890
}
5991
}
6092
});
61-
_options.AddSecurityRequirement(new OpenApiSecurityRequirement
93+
Options.AddSecurityRequirement(new OpenApiSecurityRequirement
6294
{
6395
{
6496
new OpenApiSecurityScheme

0 commit comments

Comments
 (0)