Skip to content

Commit

Permalink
Merge pull request #42 from wemogy/swagger/default-path
Browse files Browse the repository at this point in the history
Add option to not publish swagger files
  • Loading branch information
robinmanuelthiel authored Oct 17, 2023
2 parents 8074768 + 475d22c commit 9cac29f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/Wemogy.AspNet/Swagger/OpenApiConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.OpenApi.Models;

namespace Wemogy.AspNet.Swagger;

public class OpenApiConfig
{
public OpenApiInfo OpenApiInfo { get; }

/// <summary>
/// Determines, if the OpenAPI specification should be published to the UI.
/// </summary>
public bool Publish { get; }

public OpenApiConfig(OpenApiInfo openApiInfo, bool publish)
{
OpenApiInfo = openApiInfo;
Publish = publish;
}
}
15 changes: 11 additions & 4 deletions src/Wemogy.AspNet/Swagger/OpenApiEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ public class OpenApiEnvironment
{
public string Version { get; }
public string PathToXmlDocumentationFile { get; private set; }
public Dictionary<string, OpenApiInfo> OpenApiGroups { get; }
public Dictionary<string, OpenApiConfig> OpenApiGroups { get; }
public bool RemoveDtoSuffix { get; }
public List<Action<SwaggerGenOptions>> SwaggerGenOptions { get; }

public OpenApiEnvironment(string version, string pathToXmlDocumentationFile)
{
Version = version;
PathToXmlDocumentationFile = pathToXmlDocumentationFile;
OpenApiGroups = new Dictionary<string, OpenApiInfo>();
OpenApiGroups = new Dictionary<string, OpenApiConfig>();
RemoveDtoSuffix = true;
SwaggerGenOptions = new List<Action<SwaggerGenOptions>> { x => x.SupportNonNullableReferenceTypes() };
}

public OpenApiEnvironment WithApiGroup(string name, string title, string description)
/// <summary>
/// Adds a new API group to the OpenAPI specification.
/// </summary>
/// <param name="name">Name of the OpenAPI spec file (no special chars)</param>
/// <param name="title">Title of the OpenAPI spec.</param>
/// <param name="description">Description of the OpenAPI spec.</param>
/// <param name="publish">Publish OpenAPI specification to the UI.</param>
public OpenApiEnvironment WithApiGroup(string name, string title, string description, bool publish = true)
{
var info = new OpenApiInfo
{
Title = title,
Version = Version,
Description = description
};
OpenApiGroups.Add(name, info);
OpenApiGroups.Add(name, new OpenApiConfig(info, publish));
SwaggerGenOptions.Add(c => c.AddDefaults(name, info, PathToXmlDocumentationFile, RemoveDtoSuffix));
return this;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public static void UseDefaultSwagger(this IApplicationBuilder applicationBuilder
{
foreach (var group in environment.OpenApiGroups)
{
c.SwaggerEndpoint($"/swagger/{group.Key}/swagger.json", $"{group.Value.Title} - {group.Value.Version}");
// Only publish groups that are marked as publishable
if (group.Value.Publish)
{
c.SwaggerEndpoint($"/swagger/{group.Key}/swagger.json", $"{group.Value.OpenApiInfo.Title} - {group.Value.OpenApiInfo.Version}");
}
}
});
}
Expand Down

0 comments on commit 9cac29f

Please sign in to comment.