Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml support added #66

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace AzureFunctions.Extensions.Swashbuckle
public interface ISwashBuckleClient
{
string RoutePrefix { get; }
Stream GetSwaggerDocument(string host, string documentName = "v1");
Stream GetSwaggerJsonDocument(string host, string documentName = "v1");
Stream GetSwaggerYamlDocument(string host, string documentName = "v1");
Stream GetSwaggerUi(string swaggerUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ public SwashBuckleClient(SwashbuckleConfig config)
_config = config;
}

public Stream GetSwaggerDocument(string host, string documentName = "v1")
public Stream GetSwaggerJsonDocument(string host, string documentName = "v1")
{
return _config.GetSwaggerDocument(host, documentName);
return _config.GetSwaggerJsonDocument(host, documentName);
}

public Stream GetSwaggerYamlDocument(string host, string documentName = "v1")
{
return _config.GetSwaggerYamlDocument(host, documentName);
}

public Stream GetSwaggerUi(string swaggerUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.XPath;
Expand All @@ -28,7 +27,6 @@
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;


namespace AzureFunctions.Extensions.Swashbuckle.SwashBuckle
{
[Extension("Swashbuckle", "Swashbuckle")]
Expand Down Expand Up @@ -159,14 +157,21 @@ public string GetSwaggerUIContent(string swaggerUrl)
return html.Replace("{url}", swaggerUrl);
}

public Stream GetSwaggerDocument(string host, string documentName = "v1")
public Stream GetSwaggerJsonDocument(string host, string documentName = "v1")
{
var swaggerProvider = _serviceProvider.GetRequiredService<ISwaggerProvider>();
var document = swaggerProvider.GetSwagger(documentName, host, string.Empty);
return SerializeJsonDocument(document);
}

public Stream GetSwaggerYamlDocument(string host, string documentName = "v1")
{
var swaggerProvider = _serviceProvider.GetRequiredService<ISwaggerProvider>();
var document = swaggerProvider.GetSwagger(documentName, host, string.Empty);
return SerializeDocument(document);
return SerializeYamlDocument(document);
}

private MemoryStream SerializeDocument(OpenApiDocument document)
private MemoryStream SerializeJsonDocument(OpenApiDocument document)
{
var memoryStream = new MemoryStream();
document.SerializeAsJson(memoryStream,
Expand All @@ -178,6 +183,18 @@ private MemoryStream SerializeDocument(OpenApiDocument document)
return memoryStream;
}

private MemoryStream SerializeYamlDocument(OpenApiDocument document)
{
var memoryStream = new MemoryStream();
document.SerializeAsYaml(memoryStream,
_swaggerOptions.SpecVersion == OpenApiSpecVersion.OpenApi2_0 ?
OpenApiSpecVersion.OpenApi2_0 :
OpenApiSpecVersion.OpenApi3_0);

memoryStream.Position = 0;
return memoryStream;
}

private static void AddSwaggerDocument(SwaggerGenOptions options, SwaggerDocument document)
{
options.SwaggerDoc(document.Name, new OpenApiInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@ namespace AzureFunctions.Extensions.Swashbuckle
{
public static class SwashBuckleClientExtension
{
public static HttpResponseMessage CreateSwaggerDocumentResponse(
public static HttpResponseMessage CreateSwaggerJsonDocumentResponse(
this ISwashBuckleClient client,
HttpRequestMessage requestMessage,
string documentName = "v1")
{
var host = GetBaseUri(client, requestMessage);

var stream = client.GetSwaggerDocument(host, documentName);
var stream = client.GetSwaggerJsonDocument(host, documentName);
var reader = new StreamReader(stream);
var document = reader.ReadToEnd();

var response = new HttpResponseMessage(HttpStatusCode.OK)
{
RequestMessage = requestMessage,
Content = new StringContent(document, Encoding.UTF8, "application/json")
};

return response;
}

public static HttpResponseMessage CreateSwaggerYamlDocumentResponse(
this ISwashBuckleClient client,
HttpRequestMessage requestMessage,
string documentName = "v1")
{
var host = GetBaseUri(client, requestMessage);

var stream = client.GetSwaggerYamlDocument(host, documentName);
var reader = new StreamReader(stream);
var document = reader.ReadToEnd();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ namespace TestFunction
public static class SwaggerController
{
[SwaggerIgnore]
[FunctionName("Swagger")]
public static Task<HttpResponseMessage> Swagger(
[FunctionName("SwaggerJson")]
public static Task<HttpResponseMessage> SwaggerJson(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "Swagger/json")]
HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
{
return Task.FromResult(swashBuckleClient.CreateSwaggerDocumentResponse(req));
return Task.FromResult(swashBuckleClient.CreateSwaggerJsonDocumentResponse(req));
}

[SwaggerIgnore]
[FunctionName("SwaggerYaml")]
public static Task<HttpResponseMessage> SwaggerYaml(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "Swagger/yaml")]
HttpRequestMessage req,
[SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
{
return Task.FromResult(swashBuckleClient.CreateSwaggerYamlDocumentResponse(req));
}

[SwaggerIgnore]
Expand Down