Skip to content

Commit

Permalink
https://github.com/vitalybibikov/AzureExtensions.Swashbuckle?tab=read…
Browse files Browse the repository at this point in the history
…me-ov-file
  • Loading branch information
pavel-zhur committed Jul 18, 2024
1 parent 63c3906 commit 9d68838
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyDB.Index.Analysis.Em.Models;
using System.Net;
using HarmonyDB.Index.Analysis.Em.Models;
using HarmonyDB.Index.Analysis.Em.Services;
using HarmonyDB.Index.Analysis.Tools;
using HarmonyDB.Index.Api.Client;
Expand Down Expand Up @@ -35,6 +36,7 @@ public TonalitiesLoops(ILoggerFactory loggerFactory, SecurityContext securityCon
_options = options.Value;
}

[ProducesResponseType<LoopsResponse>(200)]
[Function(IndexApiUrls.VExternal1TonalitiesLoops)]
public Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req, [FromBody] LoopsRequest request)
=> RunHandler(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="AzureExtensions.Swashbuckle" Version="4.0.3" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
Expand Down
71 changes: 70 additions & 1 deletion HarmonyDB.Index/HarmonyDB.Index.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Reflection;
using AzureFunctions.Extensions.Swashbuckle;
using AzureFunctions.Extensions.Swashbuckle.Settings;
using HarmonyDB.Index.Api.Client;
using HarmonyDB.Index.Api.Models;
using HarmonyDB.Index.Api.Services;
Expand All @@ -8,11 +11,14 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Nito.AsyncEx;
using OneShelf.Authorization.Api.Client;
using OneShelf.Collectives.Api.Client;
using OneShelf.Common.Api;
using OneShelf.Common.Api.WithAuthorization;
using Swashbuckle.AspNetCore.SwaggerGen;

var host = new HostBuilder()
.ConfigureApi()
Expand All @@ -31,7 +37,70 @@
.AddIndexBusinessLogic(context.Configuration)
.AddSecurityContext()
.Configure<IndexApiOptions>(o => context.Configuration.GetSection(nameof(IndexApiOptions)).Bind(o));

services.AddSwashBuckle(opts =>
{
// If you want to add Newtonsoft support insert next line
// opts.AddNewtonsoftSupport = true;
opts.RoutePrefix = "api";
opts.SpecVersion = OpenApiSpecVersion.OpenApi3_0;
opts.AddCodeParameter = true;
opts.PrependOperationWithRoutePrefix = true;
opts.XmlPath = "TestFunction.xml";
opts.Documents = new[]
{
new SwaggerDocument
{
Name = "v1",
Title = "Swagger document",
Description = "Swagger test document",
Version = "v2"
},
new SwaggerDocument
{
Name = "v2",
Title = "Swagger document 2",
Description = "Swagger test document 2",
Version = "v2"
}
};
opts.Title = "Swagger Test";
//opts.OverridenPathToSwaggerJson = new Uri("http://localhost:7071/api/Swagger/json");
opts.ConfigureSwaggerGen = x =>
{
//custom operation example
x.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)
? methodInfo.Name
: new Guid().ToString());

//custom filter example
//x.DocumentFilter<RemoveSchemasFilter>();

//oauth2
x.AddSecurityDefinition("oauth2",
new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("https://your.idserver.net/connect/authorize"),
Scopes = new Dictionary<string, string>
{
{ "api.read", "Access read operations" },
{ "api.write", "Access write operations" }
}
}
}
});
};

// set up your client ID if your API is protected
opts.ClientId = "your.client.id";
opts.OAuth2RedirectPath = "http://localhost:7071/api/swagger/oauth2-redirect";
});
})
.Build();

host.Run();
host.Run();
57 changes: 57 additions & 0 deletions HarmonyDB.Index/HarmonyDB.Index.Api/SwaggerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using AzureFunctions.Extensions.Swashbuckle;
using Microsoft.Azure.Functions.Worker;
using Swashbuckle.AspNetCore.Annotations;

public class SwaggerController
{
private readonly ISwashBuckleClient swashBuckleClient;

public SwaggerController(ISwashBuckleClient swashBuckleClient)
{
this.swashBuckleClient = swashBuckleClient;
}

[SwaggerIgnore]
[Function("SwaggerJson")]
public async Task<HttpResponseData> SwaggerJson(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "Swagger/json")]
HttpRequestData req)
{
return await this.swashBuckleClient.CreateSwaggerJsonDocumentResponse(req);
}

[SwaggerIgnore]
[Function("SwaggerYaml")]
public async Task<HttpResponseData> SwaggerYaml(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "Swagger/yaml")]
HttpRequestData req)
{
return await this.swashBuckleClient.CreateSwaggerYamlDocumentResponse(req);
}

[SwaggerIgnore]
[Function("SwaggerUi")]
public async Task<HttpResponseData> SwaggerUi(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "Swagger/ui")]
HttpRequestData req)
{
return await this.swashBuckleClient.CreateSwaggerUIResponse(req, "swagger/json");
}

/// <summary>
/// This is only needed for OAuth2 client. This redirecting document is normally served
/// as a static content. Functions don't provide this out of the box, so we serve it here.
/// Don't forget to set OAuth2RedirectPath configuration option to reflect this route.
/// </summary>
/// <param name="req"></param>
/// <param name="swashBuckleClient"></param>
/// <returns></returns>
[SwaggerIgnore]
[Function("SwaggerOAuth2Redirect")]
public async Task<HttpResponseData> SwaggerOAuth2Redirect(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "swagger/oauth2-redirect")]
HttpRequestData req)
{
return await this.swashBuckleClient.CreateSwaggerOAuth2RedirectResponse(req);
}
}

0 comments on commit 9d68838

Please sign in to comment.