-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported Tingle.AspNetCore.Swagger (#220)
- Loading branch information
1 parent
c289a62
commit 6318f23
Showing
54 changed files
with
2,604 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Tingle.AspNetCore.Swagger/Extensions/IServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#if NET8_0_OR_GREATER | ||
using Asp.Versioning.ApiExplorer; | ||
#endif | ||
using Microsoft.Extensions.Options; | ||
#if NET8_0_OR_GREATER | ||
using Microsoft.OpenApi.Models; | ||
#endif | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Tingle.AspNetCore.Swagger.Filters; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extensions for <see cref="IServiceCollection"/> | ||
/// </summary> | ||
public static class IServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds conversion of XML comments extracted for Swagger to markdown. | ||
/// </summary> | ||
/// <param name="services">the service collection to use</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSwaggerXmlToMarkdown(this IServiceCollection services) | ||
{ | ||
return services.AddTransient<IPostConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerGenXmlToMarkdown>(); | ||
} | ||
|
||
/// <summary> | ||
/// Adds enum descriptions. | ||
/// This should be called after all XML documents have been added. | ||
/// </summary> | ||
/// <param name="services">the service collection to use</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSwaggerEnumDescriptions(this IServiceCollection services) | ||
{ | ||
return services.AddTransient<IPostConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerGenEnumDescriptions>(); | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
|
||
/// <summary> | ||
/// Adds swagger documents for api version descriptions declared in code. | ||
/// This is resolved through <see cref="IApiVersionDescriptionProvider.ApiVersionDescriptions"/> | ||
/// </summary> | ||
/// <param name="services">the service collection to use</param> | ||
/// <param name="title">the title of the documents</param> | ||
/// <param name="description">the description of the documents, if any</param> | ||
/// <param name="deprecationSuffix">the suffix to add for deprecated versions</param> | ||
/// <param name="skipDeprecated">set true to skip versions marked as deprecated</param> | ||
/// <param name="customize">an action to customize the created instance of <see cref="OpenApiInfo"/> before adding it</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddSwaggerDocsAutoDiscovery(this IServiceCollection services, | ||
string? title = null, | ||
string? description = null, | ||
bool skipDeprecated = false, | ||
string deprecationSuffix = "[deprecated]", | ||
Action<OpenApiInfo>? customize = null) | ||
{ | ||
return services.AddTransient<IConfigureOptions<SwaggerGenOptions>>(serviceProvider => | ||
{ | ||
void configureAllVersions(IEnumerable<ApiVersionDescription> versions, SwaggerGenOptions options) | ||
{ | ||
options.AddDocuments(versions: versions, | ||
title: title, | ||
description: description, | ||
skipDeprecated: skipDeprecated, | ||
deprecationSuffix: deprecationSuffix, | ||
customize: customize); | ||
} | ||
var provider = serviceProvider.GetRequiredService<IApiVersionDescriptionProvider>(); | ||
return new ConfigureSwaggerGenAddDocs(provider, configureAllVersions); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Configures the Swagger generation options. | ||
/// </summary> | ||
/// <remarks> | ||
/// This allows API versioning to define a Swagger document per API version after the | ||
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container. | ||
/// </remarks> | ||
/// <param name="provider">the <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param> | ||
/// <param name="configure">the action to call when configuring</param> | ||
internal class ConfigureSwaggerGenAddDocs(IApiVersionDescriptionProvider provider, Action<IEnumerable<ApiVersionDescription>, SwaggerGenOptions> configure) | ||
: IConfigureOptions<SwaggerGenOptions> | ||
{ | ||
/// <inheritdoc /> | ||
public void Configure(SwaggerGenOptions options) => configure?.Invoke(provider.ApiVersionDescriptions, options); | ||
} | ||
|
||
#endif | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Tingle.AspNetCore.Swagger/Extensions/SwaggerGenExtensions.ReDoc.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Tingle.AspNetCore.Swagger; | ||
using Tingle.AspNetCore.Swagger.Filters.Documents; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extensions for <see cref="SwaggerGenOptions"/> | ||
/// </summary> | ||
public static partial class SwaggerGenExtensions | ||
{ | ||
/// <summary> | ||
/// Add logo for ReDoc to the document using the vendor extension <c>x-logo</c> | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="logo">The logo details</param> | ||
/// <returns></returns> | ||
/// <seealso cref="ReDocLogoDocumentFilter" /> | ||
public static SwaggerGenOptions AddReDocLogo(this SwaggerGenOptions options, OpenApiReDocLogo logo) | ||
{ | ||
options.DocumentFilter<ReDocLogoDocumentFilter>(logo); | ||
return options; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/Tingle.AspNetCore.Swagger/Extensions/SwaggerGenExtensions.Versioning.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#if NET8_0_OR_GREATER | ||
using Asp.Versioning.ApiExplorer; | ||
#endif | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extensions for <see cref="SwaggerGenOptions"/> | ||
/// </summary> | ||
public static partial class SwaggerGenExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a swagger document | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="documentName">the name of the document e.g. v1</param> | ||
/// <param name="versionName">the name of the version e.g. v1-2019</param> | ||
/// <param name="title">the title of the document</param> | ||
/// <param name="description">the description of the document, if any</param> | ||
/// <param name="customize">an action to customize the created instance of <see cref="OpenApiInfo"/> before adding it</param> | ||
/// <returns></returns> | ||
public static SwaggerGenOptions AddDocument(this SwaggerGenOptions options, | ||
string documentName, | ||
string? versionName = null, | ||
string? title = null, | ||
string? description = null, | ||
Action<OpenApiInfo>? customize = null) | ||
{ | ||
var info = new OpenApiInfo | ||
{ | ||
Version = versionName ?? documentName, | ||
Title = title, | ||
Description = description, | ||
}; | ||
customize?.Invoke(info); | ||
options.SwaggerDoc(documentName, info); | ||
return options; | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
|
||
/// <summary> | ||
/// Adds a swagger document from an API version description | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="version">the API version description</param> | ||
/// <param name="title">the title of the document</param> | ||
/// <param name="description">the description of the document, if any</param> | ||
/// <param name="deprecationSuffix">the suffix to add for deprecated versions</param> | ||
/// <param name="customize">an action to customize the created instance of <see cref="OpenApiInfo"/> before adding it</param> | ||
/// <returns></returns> | ||
public static SwaggerGenOptions AddDocument(this SwaggerGenOptions options, | ||
ApiVersionDescription version, | ||
string? title = null, | ||
string? description = null, | ||
string? deprecationSuffix = "[deprecated]", | ||
Action<OpenApiInfo>? customize = null) | ||
{ | ||
var finalTitle = title; | ||
if (version.IsDeprecated) finalTitle += $" {deprecationSuffix}"; | ||
var info = new OpenApiInfo | ||
{ | ||
Version = version.ApiVersion.ToString(), | ||
Title = finalTitle, | ||
Description = description, | ||
}; | ||
customize?.Invoke(info); | ||
options.SwaggerDoc(version.GroupName, info); | ||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Adds swagger documents for each API version description provided | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <param name="versions">the versions for which to add swagger documents</param> | ||
/// <param name="title">the title of the documents</param> | ||
/// <param name="description">the description of the documents, if any</param> | ||
/// <param name="deprecationSuffix">the suffix to add for deprecated versions</param> | ||
/// <param name="skipDeprecated">set true to skip versions marked as deprecated</param> | ||
/// <param name="customize">an action to customize the created instance of <see cref="OpenApiInfo"/> before adding it</param> | ||
/// <returns></returns> | ||
public static SwaggerGenOptions AddDocuments(this SwaggerGenOptions options, | ||
IEnumerable<ApiVersionDescription> versions, | ||
string? title = null, | ||
string? description = null, | ||
bool skipDeprecated = false, | ||
string deprecationSuffix = "[deprecated]", | ||
Action<OpenApiInfo>? customize = null) | ||
{ | ||
// add a swagger document for each discovered API version | ||
foreach (var v in versions) | ||
{ | ||
// skip deprecated versions if specified | ||
if (skipDeprecated && v.IsDeprecated) continue; | ||
|
||
// add document | ||
options.AddDocument( | ||
version: v, | ||
title: title, | ||
description: description, | ||
deprecationSuffix: deprecationSuffix, | ||
customize: customize); | ||
} | ||
|
||
return options; | ||
} | ||
#endif | ||
|
||
} |
Oops, something went wrong.