Skip to content

Commit

Permalink
- Fix breaking binary changes
Browse files Browse the repository at this point in the history
- code review suggestions
  • Loading branch information
Remco Lam committed Apr 15, 2024
1 parent 2cc25bb commit b3c8c8f
Show file tree
Hide file tree
Showing 4 changed files with 1,379 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/Swashbuckle.AspNetCore.Swagger/ISwaggerDocumentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

namespace Swashbuckle.AspNetCore.Swagger
{
/// <summary>
/// Provide an implementation for this interface if you wish to customize how the open api document is exactly written.
/// SerializeDocument will be called in that case, instead of the Microsoft built in Serialize methods.
/// </summary>
public interface ISwaggerDocumentSerializer
{
/// <summary>
/// Called in the places where normally SerializeV2 or SerializeV3 on OpenApiDocument would be called.
/// </summary>
/// <param name="document">The open api document that should be serialized</param>
/// <param name="writer">The write to which the document needs to be written</param>
/// <param name="specVersion">The open api spec to serialize</param>
void SerializeDocument(OpenApiDocument document, IOpenApiWriter writer, OpenApiSpecVersion specVersion);
}
}
}
9 changes: 8 additions & 1 deletion src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Template;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;

Expand All @@ -18,6 +19,12 @@ public class SwaggerMiddleware
private readonly TemplateMatcher _requestMatcher;
private readonly ISwaggerDocumentSerializer _swaggerDocumentSerializer;

public SwaggerMiddleware(
RequestDelegate next,
SwaggerOptions options) : this (next, options, null)
{
}

public SwaggerMiddleware(
RequestDelegate next,
SwaggerOptions options,
Expand All @@ -28,7 +35,7 @@ public SwaggerMiddleware(
_requestMatcher = new TemplateMatcher(TemplateParser.Parse(_options.RouteTemplate), new RouteValueDictionary());

// Use IServiceProvider to retrieve the ISwaggerDocumentSerializer, because it is an optional service
_swaggerDocumentSerializer = serviceProvider.GetService(typeof(ISwaggerDocumentSerializer)) as ISwaggerDocumentSerializer;
_swaggerDocumentSerializer = serviceProvider?.GetService<ISwaggerDocumentSerializer>();
}

public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Writers;
using Swashbuckle.AspNetCore.Swagger;
Expand All @@ -27,6 +28,13 @@ internal class DocumentProvider : IDocumentProvider
private readonly IAsyncSwaggerProvider _swaggerProvider;
private readonly ISwaggerDocumentSerializer _swaggerDocumentSerializer;

public DocumentProvider(
IOptions<SwaggerGeneratorOptions> generatorOptions,
IOptions<SwaggerOptions> options,
IAsyncSwaggerProvider swaggerProvider
) : this(generatorOptions, options, swaggerProvider, null)
{ }

public DocumentProvider(
IOptions<SwaggerGeneratorOptions> generatorOptions,
IOptions<SwaggerOptions> options,
Expand All @@ -39,7 +47,7 @@ IServiceProvider serviceProvider
_swaggerProvider = swaggerProvider;

// Use IServiceProvider to retrieve the ISwaggerDocumentSerializer, because it is an optional service
_swaggerDocumentSerializer = serviceProvider.GetService(typeof(ISwaggerDocumentSerializer)) as ISwaggerDocumentSerializer;
_swaggerDocumentSerializer = serviceProvider?.GetService<ISwaggerDocumentSerializer>();
}

public IEnumerable<string> GetDocumentNames()
Expand Down
Loading

0 comments on commit b3c8c8f

Please sign in to comment.