Skip to content

Commit

Permalink
Merge pull request #66 from michaelmatthiaskern/feature/yaml_support
Browse files Browse the repository at this point in the history
Yaml support added
  • Loading branch information
vitalybibikov authored Mar 5, 2021
2 parents b7b7aee + 65c838a commit ad55111
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
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);
Stream GetSwaggerOAuth2Redirect();
}
Expand Down
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 GetSwaggerOAuth2Redirect()
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 @@ -182,14 +180,21 @@ public string GetSwaggerUIContent(string swaggerUrl)
.Replace("{clientId}", _swaggerOptions.ClientId);
}

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 @@ -201,6 +206,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

0 comments on commit ad55111

Please sign in to comment.