Skip to content

Commit

Permalink
Temporarily move back to old doc generation until OpenAPI gets fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlihou committed Jun 19, 2024
1 parent dc0f8a6 commit 2414944
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion FiMAdminApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
using FiMAdminApi.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -38,6 +40,49 @@
});
builder.Services.AddProblemDetails();
builder.Services.AddEndpointsApiExplorer();

// TODO: Remove when OpenAPI is working
builder.Services.AddSwaggerGen(opt =>
{
// // Add the security scheme at the document level
// var requirements = new Dictionary<string, OpenApiSecurityScheme>
// {
// ["Bearer"] = new OpenApiSecurityScheme
// {
// Type = SecuritySchemeType.Http,
// Scheme = "bearer", // "bearer" refers to the header name here
// In = ParameterLocation.Header,
// BearerFormat = "Json Web Token"
// }
// };
// document.Components ??= new OpenApiComponents();
// document.Components.SecuritySchemes = requirements;
//
// // Apply it as a requirement for all operations
// foreach (var operation in document.Paths.Values.SelectMany(path => path.Operations))
// {
// operation.Value.Security.Add(new OpenApiSecurityRequirement
// {
// [new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", Type = ReferenceType.SecurityScheme } }] = Array.Empty<string>()
// });
// }
var scheme = new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
In = ParameterLocation.Header,
BearerFormat = "JSON Web Token"
};
opt.AddSecurityDefinition("Bearer", scheme);
opt.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{ scheme, [] }
});
opt.OperationFilter<AuthorizeCheckOperationFilter>();
});

builder.Services.AddOpenApi(opt =>
{
opt.UseTransformer((doc, _, _) =>
Expand Down Expand Up @@ -119,6 +164,10 @@

// Configure the HTTP request pipeline.
app.MapOpenApi();

// TODO: Remove when OpenAPI is working
app.MapSwagger();

app.UseHttpsRedirection();
app.UseCors();

Expand All @@ -130,6 +179,7 @@
}).ExcludeFromDescription();

// Serve API documentation
// TODO: Update to `/openapi/v1.json` when OpenAPI is working
app.MapGet("/docs", () =>
{
const string resp = """
Expand All @@ -139,7 +189,7 @@
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements/styles.min.css">
</head>
<body>
<elements-api apiDescriptionUrl="/openapi/v1.json" router="hash" basePath="/docs"/>
<elements-api apiDescriptionUrl="/swagger/v1/swagger.json" router="hash" basePath="/docs"/>
</body>
</html>
""";
Expand Down Expand Up @@ -188,4 +238,31 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf
}
}
}
}

// TODO: Remove when OpenAPI is working
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
// operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });

var jwtBearerScheme = new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
};

operation.Security = new List<OpenApiSecurityRequirement>
{
new OpenApiSecurityRequirement
{
[jwtBearerScheme] = Array.Empty<string>()
}
};
}
}

0 comments on commit 2414944

Please sign in to comment.