Skip to content

Commit 7f0c58b

Browse files
Add option to not publish swagger files
1 parent af30703 commit 7f0c58b

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.OpenApi.Models;
2+
3+
namespace Wemogy.AspNet.Swagger;
4+
5+
public class OpenApiConfig
6+
{
7+
public OpenApiInfo OpenApiInfo { get; }
8+
9+
/// <summary>
10+
/// Determines, if the OpenAPI specification should be published to the UI.
11+
/// </summary>
12+
public bool Publish { get; }
13+
14+
public OpenApiConfig(OpenApiInfo openApiInfo, bool publish)
15+
{
16+
OpenApiInfo = openApiInfo;
17+
Publish = publish;
18+
}
19+
}

src/Wemogy.AspNet/Swagger/OpenApiEnvironment.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,35 @@ public class OpenApiEnvironment
1111
{
1212
public string Version { get; }
1313
public string PathToXmlDocumentationFile { get; private set; }
14-
public Dictionary<string, OpenApiInfo> OpenApiGroups { get; }
14+
public Dictionary<string, OpenApiConfig> OpenApiGroups { get; }
1515
public bool RemoveDtoSuffix { get; }
1616
public List<Action<SwaggerGenOptions>> SwaggerGenOptions { get; }
1717

1818
public OpenApiEnvironment(string version, string pathToXmlDocumentationFile)
1919
{
2020
Version = version;
2121
PathToXmlDocumentationFile = pathToXmlDocumentationFile;
22-
OpenApiGroups = new Dictionary<string, OpenApiInfo>();
22+
OpenApiGroups = new Dictionary<string, OpenApiConfig>();
2323
RemoveDtoSuffix = true;
2424
SwaggerGenOptions = new List<Action<SwaggerGenOptions>> { x => x.SupportNonNullableReferenceTypes() };
2525
}
2626

27-
public OpenApiEnvironment WithApiGroup(string name, string title, string description)
27+
/// <summary>
28+
/// Adds a new API group to the OpenAPI specification.
29+
/// </summary>
30+
/// <param name="name">Name of the OpenAPI spec file (no special chars)</param>
31+
/// <param name="title">Title of the OpenAPI spec.</param>
32+
/// <param name="description">Description of the OpenAPI spec.</param>
33+
/// <param name="publish">Publish OpenAPI specification to the UI.</param>
34+
public OpenApiEnvironment WithApiGroup(string name, string title, string description, bool publish = true)
2835
{
2936
var info = new OpenApiInfo
3037
{
3138
Title = title,
3239
Version = Version,
3340
Description = description
3441
};
35-
OpenApiGroups.Add(name, info);
42+
OpenApiGroups.Add(name, new OpenApiConfig(info, publish));
3643
SwaggerGenOptions.Add(c => c.AddDefaults(name, info, PathToXmlDocumentationFile, RemoveDtoSuffix));
3744
return this;
3845
}

src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public static void UseDefaultSwagger(this IApplicationBuilder applicationBuilder
3131
{
3232
foreach (var group in environment.OpenApiGroups)
3333
{
34-
c.SwaggerEndpoint($"/swagger/{group.Key}/swagger.json", $"{group.Value.Title} - {group.Value.Version}");
34+
// Only publish groups that are marked as publishable
35+
if (group.Value.Publish)
36+
{
37+
c.SwaggerEndpoint($"/swagger/{group.Key}/swagger.json", $"{group.Value.OpenApiInfo.Title} - {group.Value.OpenApiInfo.Version}");
38+
}
3539
}
3640
});
3741
}

0 commit comments

Comments
 (0)