Skip to content

Commit

Permalink
Replace Swashbuckle.AspNetCore with Microsoft.AspNetCore.OpenApi
Browse files Browse the repository at this point in the history
  • Loading branch information
liguori committed Nov 26, 2024
1 parent 48f38f8 commit 1b54b23
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 62 deletions.
42 changes: 42 additions & 0 deletions src/Savings.API/OpenApi/DocumentSecuritySchemeTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Savings.API.Authentication;
using Savings.Model;

namespace Savings.API.OpenApi
{
internal sealed class DocumentSecuritySchemeTransformer(IConfiguration conf) : IOpenApiDocumentTransformer
{

public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken)
{
var authenticationToUse = conf["AuthenticationToUse"];

document.Info = new OpenApiInfo { Title = "Savings", Version = "v1" };
if (authenticationToUse == AuthenticationToUse.AzureAD)
{
document.Components = new OpenApiComponents();
document.Components.SecuritySchemes.Add(ApiKeyAuthOptions.ApiKeySchemaName, new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.OAuth2
});
}
else if (authenticationToUse == AuthenticationToUse.ApiKey)
{
document.Components = new OpenApiComponents();
document.Components.SecuritySchemes.Add(ApiKeyAuthOptions.ApiKeySchemaName, new OpenApiSecurityScheme
{
Description = "Api key needed to access the endpoints. " + ApiKeyAuthOptions.HeaderName + ": My_API_Key",
In = ParameterLocation.Header,
Name = ApiKeyAuthOptions.HeaderName,
Type = SecuritySchemeType.ApiKey
});
}

return Task.CompletedTask;
}
}
}
52 changes: 52 additions & 0 deletions src/Savings.API/OpenApi/OperationSecurityTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Models;
using Savings.API.Authentication;
using Savings.Model;

namespace Savings.API.OpenApi
{
internal sealed class OperationSecurityTransformer(IConfiguration conf) : IOpenApiOperationTransformer
{
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
var authenticationToUse = conf["AuthenticationToUse"];

if (authenticationToUse == AuthenticationToUse.AzureAD)
{
operation.Security = [new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
}];
}
else if (authenticationToUse == AuthenticationToUse.ApiKey)
{
operation.Security = [new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Name = ApiKeyAuthOptions.HeaderName,
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = ApiKeyAuthOptions.ApiKeySchemaName
},
},
new string[] {}
}
}];
}
return Task.CompletedTask;
}
}
}
70 changes: 9 additions & 61 deletions src/Savings.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.OpenApi.Models;
using Savings.API.Authentication;
using Savings.API.Infrastructure;
using Savings.API.OpenApi;
using Savings.API.Services;
using Savings.API.Services.Abstract;
using Savings.Model;
Expand Down Expand Up @@ -48,71 +49,17 @@

builder.Services.AddTransient<IProjectionCalculator, ProjectionCalculator>();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
builder.Services.AddOpenApi(options =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Savings", Version = "v1" });

if (authenticationToUse == AuthenticationToUse.AzureAD)
{
c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
}
else if (authenticationToUse == AuthenticationToUse.ApiKey)
{
//Add API Key Informations
c.AddSecurityDefinition(ApiKeyAuthOptions.ApiKeySchemaName, new OpenApiSecurityScheme
{
Description = "Api key needed to access the endpoints. " + ApiKeyAuthOptions.HeaderName + ": My_API_Key",
In = ParameterLocation.Header,
Name = ApiKeyAuthOptions.HeaderName,
Type = SecuritySchemeType.ApiKey
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Name = ApiKeyAuthOptions.HeaderName,
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = ApiKeyAuthOptions.ApiKeySchemaName
},
},
new string[] {}
}
});
}
options.AddDocumentTransformer<DocumentSecuritySchemeTransformer>();
options.AddOperationTransformer<OperationSecurityTransformer>();
});

builder.Services.AddDbContext<SavingsContext>(
options => options.UseSqlite($"Data Source={builder.Configuration["DatabasePath"]}", sqlOpt =>
builder.Services.AddDbContext<SavingsContext>(options => options.UseSqlite($"Data Source={builder.Configuration["DatabasePath"]}", sqlOpt =>
{
sqlOpt.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
})
);
);

builder.Services.AddCors(options =>
{
Expand All @@ -125,8 +72,9 @@
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();

app.UseSwaggerUI(c => c.SwaggerEndpoint("/openapi/v1.json", "Savings API"));
app.UseDeveloperExceptionPage();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Savings.API/Savings.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -18,7 +19,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="9.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Savings.Model\Savings.Model.csproj" />
Expand Down

0 comments on commit 1b54b23

Please sign in to comment.