-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customized document serialization support (#2677)
Added new `ISwaggerDocumentSerializer` interface for custom serialization support.
- Loading branch information
Showing
24 changed files
with
552 additions
and
336 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerOptionsExtensions.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,29 @@ | ||
using System; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extensions for helping with configuring instances of <see cref="SwaggerOptions"/>. | ||
/// </summary> | ||
public static class SwaggerOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Sets a custom Swagger document serializer to use. | ||
/// </summary> | ||
/// <remarks>For the CLI tool to be able to use this, this needs to be configured for use in the service collection of your application.</remarks> | ||
/// <typeparam name="TDocumentSerializer">The type of the custom Swagger document serializer implementation.</typeparam> | ||
/// <param name="swaggerOptions">The options to configure the serializer for.</param> | ||
/// <param name="constructorParameters">The parameters to pass into the constructor of the custom Swagger document serializer implementation.</param> | ||
public static void SetCustomDocumentSerializer<TDocumentSerializer>( | ||
this SwaggerOptions swaggerOptions, | ||
params object[] constructorParameters) | ||
where TDocumentSerializer : ISwaggerDocumentSerializer | ||
{ | ||
if (swaggerOptions == null) | ||
{ | ||
throw new ArgumentNullException(nameof(swaggerOptions)); | ||
} | ||
swaggerOptions.CustomDocumentSerializer = (TDocumentSerializer)Activator.CreateInstance(typeof(TDocumentSerializer), constructorParameters); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Swashbuckle.AspNetCore.Swagger/DependencyInjection/SwaggerServiceCollectionExtensions.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,22 @@ | ||
using System; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extensions to configure dependencies for Swagger. | ||
/// </summary> | ||
public static class SwaggerServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Configures Swagger options in the specified service collection. | ||
/// </summary> | ||
/// <param name="services">The service collection to configure the Swagger options for.</param> | ||
/// <param name="setupAction">A delegate to a method to use to configure the Swagger options.</param> | ||
public static void ConfigureSwagger( | ||
this IServiceCollection services, | ||
Action<SwaggerOptions> setupAction) | ||
{ | ||
services.Configure(setupAction); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Swashbuckle.AspNetCore.Swagger/ISwaggerDocumentSerializer.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,20 @@ | ||
using Microsoft.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
using Microsoft.OpenApi.Writers; | ||
|
||
namespace Swashbuckle.AspNetCore.Swagger | ||
{ | ||
/// <summary> | ||
/// Provide an implementation for this interface if you wish to customize how the OpenAPI document is written. | ||
/// </summary> | ||
public interface ISwaggerDocumentSerializer | ||
{ | ||
/// <summary> | ||
/// Serializes an OpenAPI document. | ||
/// </summary> | ||
/// <param name="document">The OpenAPI document that should be serialized.</param> | ||
/// <param name="writer">The writer to which the document needs to be written.</param> | ||
/// <param name="specVersion">The OpenAPI specification version to serialize as.</param> | ||
void SerializeDocument(OpenApiDocument document, IOpenApiWriter writer, OpenApiSpecVersion specVersion); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -31,4 +31,4 @@ public class FilterDescriptor | |
|
||
public object[] Arguments { get; set; } | ||
} | ||
} | ||
} |
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
Oops, something went wrong.