Skip to content

Commit

Permalink
Added ISwaggerDocumentSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Remco Lam committed Jun 29, 2023
1 parent 8f363f7 commit 437489e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/Swashbuckle.AspNetCore.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static int Main(string[] args)
// 3) Retrieve Swagger via configured provider
var swaggerProvider = serviceProvider.GetRequiredService<ISwaggerProvider>();
var swaggerDocumentSerializer = serviceProvider.GetService<ISwaggerDocumentSerializer>();
var swagger = swaggerProvider.GetSwagger(
namedArgs["swaggerdoc"],
namedArgs.ContainsKey("--host") ? namedArgs["--host"] : null,
Expand All @@ -106,9 +107,24 @@ public static int Main(string[] args)
writer = new OpenApiJsonWriter(streamWriter);
if (namedArgs.ContainsKey("--serializeasv2"))
swagger.SerializeAsV2(writer);
{
if (swaggerDocumentSerializer != null)
{
swaggerDocumentSerializer.SerializeDocument(swagger, writer, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi2_0);
}
else
{
swagger.SerializeAsV2(writer);
}
}
else if (swaggerDocumentSerializer != null)
{
swaggerDocumentSerializer.SerializeDocument(swagger, writer, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0);
}
else
{
swagger.SerializeAsV3(writer);
}
if (outputPath != null)
Console.WriteLine($"Swagger JSON/YAML successfully written to {outputPath}");
Expand Down
11 changes: 11 additions & 0 deletions src/Swashbuckle.AspNetCore.Swagger/ISwaggerDocumentSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;

namespace Swashbuckle.AspNetCore.Swagger
{
public interface ISwaggerDocumentSerializer
{
void SerializeDocument(OpenApiDocument document, IOpenApiWriter writer, OpenApiSpecVersion specVersion);
}
}
40 changes: 36 additions & 4 deletions src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -15,14 +16,19 @@ public class SwaggerMiddleware
private readonly RequestDelegate _next;
private readonly SwaggerOptions _options;
private readonly TemplateMatcher _requestMatcher;
private readonly ISwaggerDocumentSerializer _swaggerDocumentSerializer;

public SwaggerMiddleware(
RequestDelegate next,
SwaggerOptions options)
SwaggerOptions options,
IServiceProvider serviceProvider)
{
_next = next;
_options = options ?? new SwaggerOptions();
_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;
}

public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Expand Down Expand Up @@ -97,7 +103,20 @@ private async Task RespondWithSwaggerJson(HttpResponse response, OpenApiDocument
using (var textWriter = new StringWriter(CultureInfo.InvariantCulture))
{
var jsonWriter = new OpenApiJsonWriter(textWriter);
if (_options.SerializeAsV2) swagger.SerializeAsV2(jsonWriter); else swagger.SerializeAsV3(jsonWriter);
if (_options.SerializeAsV2)
{
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, jsonWriter, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi2_0);
else
swagger.SerializeAsV2(jsonWriter);
}
else
{
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, jsonWriter, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0);
else
swagger.SerializeAsV3(jsonWriter);
}

await response.WriteAsync(textWriter.ToString(), new UTF8Encoding(false));
}
Expand All @@ -111,7 +130,20 @@ private async Task RespondWithSwaggerYaml(HttpResponse response, OpenApiDocument
using (var textWriter = new StringWriter(CultureInfo.InvariantCulture))
{
var yamlWriter = new OpenApiYamlWriter(textWriter);
if (_options.SerializeAsV2) swagger.SerializeAsV2(yamlWriter); else swagger.SerializeAsV3(yamlWriter);
if (_options.SerializeAsV2)
{
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, yamlWriter, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi2_0);
else
swagger.SerializeAsV2(yamlWriter);
}
else
{
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, yamlWriter, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0);
else
swagger.SerializeAsV3(yamlWriter);
}

await response.WriteAsync(textWriter.ToString(), new UTF8Encoding(false));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
Expand All @@ -24,15 +25,21 @@ internal class DocumentProvider : IDocumentProvider
private readonly SwaggerGeneratorOptions _generatorOptions;
private readonly SwaggerOptions _options;
private readonly IAsyncSwaggerProvider _swaggerProvider;
private readonly ISwaggerDocumentSerializer _swaggerDocumentSerializer;

public DocumentProvider(
IOptions<SwaggerGeneratorOptions> generatorOptions,
IOptions<SwaggerOptions> options,
IAsyncSwaggerProvider swaggerProvider)
IAsyncSwaggerProvider swaggerProvider,
IServiceProvider serviceProvider
)
{
_generatorOptions = generatorOptions.Value;
_options = options.Value;
_swaggerProvider = swaggerProvider;

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

public IEnumerable<string> GetDocumentNames()
Expand All @@ -47,11 +54,17 @@ public async Task GenerateAsync(string documentName, TextWriter writer)
var jsonWriter = new OpenApiJsonWriter(writer);
if (_options.SerializeAsV2)
{
swagger.SerializeAsV2(jsonWriter);
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, jsonWriter, OpenApi.OpenApiSpecVersion.OpenApi2_0);
else
swagger.SerializeAsV2(jsonWriter);
}
else
{
swagger.SerializeAsV3(jsonWriter);
if (_swaggerDocumentSerializer != null)
_swaggerDocumentSerializer.SerializeDocument(swagger, jsonWriter, OpenApi.OpenApiSpecVersion.OpenApi3_0);
else
swagger.SerializeAsV3(jsonWriter);
}
}
}
Expand Down

0 comments on commit 437489e

Please sign in to comment.