Skip to content

Commit 39a038d

Browse files
committed
Update OpenAPI and Swagger configurations
- Replace `Swashbuckle.AspNetCore` with `Microsoft.AspNetCore.OpenApi` and `Swashbuckle.AspNetCore.SwaggerUI` in project files for `ApiKeySample`, `BasicAuthenticationSample`, and `JwtBearerSample`, upgrading versions to `9.0.4` and `9.0.8` respectively. - Refactor `Program.cs` to use `AddOpenApi` and streamline Swagger UI setup with `MapOpenApi`. - Replace Swagger annotations in `AuthController`, `MeController`, and `PeopleController` with `EndpointDescription`. - Update `SimpleAuthentication` project to reflect new OpenAPI version. - Enhance API documentation capabilities and ensure compatibility with latest libraries.
1 parent 869768b commit 39a038d

File tree

18 files changed

+89
-135
lines changed

18 files changed

+89
-135
lines changed
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3-
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
5-
<Nullable>enable</Nullable>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
11-
</ItemGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.4" />
12+
</ItemGroup>
1213

13-
<ItemGroup>
14-
<ProjectReference Include="..\..\..\src\SimpleAuthentication.Swashbuckle\SimpleAuthentication.Swashbuckle.csproj" />
15-
<ProjectReference Include="..\..\..\src\SimpleAuthentication\SimpleAuthentication.csproj" />
16-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\src\SimpleAuthentication.Swashbuckle\SimpleAuthentication.Swashbuckle.csproj" />
16+
<ProjectReference Include="..\..\..\src\SimpleAuthentication\SimpleAuthentication.csproj" />
17+
</ItemGroup>
1718

1819
</Project>

samples/Controllers/ApiKeySample/Program.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232

3333
builder.Services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
3434

35-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
36-
builder.Services.AddEndpointsApiExplorer();
37-
38-
builder.Services.AddSwaggerGen(options =>
35+
builder.Services.AddOpenApi(options =>
3936
{
4037
options.AddSimpleAuthentication(builder.Configuration);
4138
});
@@ -45,18 +42,15 @@
4542
// Configure the HTTP request pipeline.
4643
app.UseHttpsRedirection();
4744

48-
if (!app.Environment.IsDevelopment())
49-
{
50-
app.UseExceptionHandler();
51-
}
52-
45+
app.UseExceptionHandler();
5346
app.UseStatusCodePages();
5447

55-
if (app.Environment.IsDevelopment())
48+
app.MapOpenApi();
49+
50+
app.UseSwaggerUI(options =>
5651
{
57-
app.UseSwagger();
58-
app.UseSwaggerUI();
59-
}
52+
options.SwaggerEndpoint("/openapi/v1.json", app.Environment.ApplicationName);
53+
});
6054

6155
app.UseAuthentication();
6256
app.UseAuthorization();
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

3-
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
5-
<Nullable>enable</Nullable>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
88

9-
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
11-
</ItemGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.4" />
12+
</ItemGroup>
1213

13-
<ItemGroup>
14-
<ProjectReference Include="..\..\..\src\SimpleAuthentication.Swashbuckle\SimpleAuthentication.Swashbuckle.csproj" />
15-
<ProjectReference Include="..\..\..\src\SimpleAuthentication\SimpleAuthentication.csproj" />
16-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\..\src\SimpleAuthentication.Swashbuckle\SimpleAuthentication.Swashbuckle.csproj" />
16+
<ProjectReference Include="..\..\..\src\SimpleAuthentication\SimpleAuthentication.csproj" />
17+
</ItemGroup>
1718

1819
</Project>

samples/Controllers/BasicAuthenticationSample/Program.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333

3434
builder.Services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
3535

36-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
37-
builder.Services.AddEndpointsApiExplorer();
38-
39-
builder.Services.AddSwaggerGen(options =>
36+
builder.Services.AddOpenApi(options =>
4037
{
4138
options.AddSimpleAuthentication(builder.Configuration);
4239
});
@@ -46,18 +43,15 @@
4643
// Configure the HTTP request pipeline.
4744
app.UseHttpsRedirection();
4845

49-
if (!app.Environment.IsDevelopment())
50-
{
51-
app.UseExceptionHandler();
52-
}
53-
46+
app.UseExceptionHandler();
5447
app.UseStatusCodePages();
5548

56-
if (app.Environment.IsDevelopment())
49+
app.MapOpenApi();
50+
51+
app.UseSwaggerUI(options =>
5752
{
58-
app.UseSwagger();
59-
app.UseSwaggerUI();
60-
}
53+
options.SwaggerEndpoint("/openapi/v1.json", app.Environment.ApplicationName);
54+
});
6155

6256
app.UseAuthentication();
6357
app.UseAuthorization();

samples/Controllers/JwtBearerSample/Controllers/AuthController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Security.Claims;
33
using Microsoft.AspNetCore.Mvc;
44
using SimpleAuthentication.JwtBearer;
5-
using Swashbuckle.AspNetCore.Annotations;
65

76
namespace JwtBearerSample.Controllers;
87

@@ -14,7 +13,7 @@ public class AuthController(IJwtBearerService jwtBearerService) : ControllerBase
1413
[HttpPost("login")]
1514
[ProducesResponseType<LoginResponse>(StatusCodes.Status200OK)]
1615
[ProducesDefaultResponseType]
17-
[SwaggerOperation(description: "Insert permissions in the scope property (for example: 'profile people:admin')")]
16+
[EndpointDescription(description: "Insert permissions in the scope property (for example: 'profile people:admin')")]
1817
public async Task<ActionResult<LoginResponse>> Login(LoginRequest loginRequest, DateTime? expiration = null)
1918
{
2019
// Check for login rights...

samples/Controllers/JwtBearerSample/Controllers/MeController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
44
using SimpleAuthentication.Permissions;
5-
using Swashbuckle.AspNetCore.Annotations;
65

76
namespace JwtBearerSample.Controllers;
87

@@ -16,7 +15,7 @@ public class MeController : ControllerBase
1615
[HttpGet]
1716
[ProducesResponseType<User>(StatusCodes.Status200OK)]
1817
[ProducesDefaultResponseType]
19-
[SwaggerOperation(description: "This endpoint requires the 'profile' permission")]
18+
[EndpointDescription(description: "This endpoint requires the 'profile' permission")]
2019
public ActionResult<User> Get()
2120
=> new User(User.Identity!.Name);
2221
}

samples/Controllers/JwtBearerSample/Controllers/PeopleController.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
44
using SimpleAuthentication.Permissions;
5-
using Swashbuckle.AspNetCore.Annotations;
65

76
namespace JwtBearerSample.Controllers;
87

@@ -14,35 +13,35 @@ public class PeopleController : ControllerBase
1413
{
1514
[Authorize(Policy = "PeopleRead")] // [Permissions(Permissions.PeopleRead, Permissions.PeopleAdmin)]
1615
[HttpGet]
17-
[SwaggerOperation(description: $"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions")]
16+
[EndpointDescription(description: $"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions")]
1817
public IActionResult GetList() => NoContent();
1918

2019
[Authorize(Policy = "PeopleRead")] // [Permissions(Permissions.PeopleRead, Permissions.PeopleAdmin)]
2120
[HttpGet("{id:int}")]
2221
[ProducesResponseType(StatusCodes.Status204NoContent)]
2322
[ProducesDefaultResponseType]
24-
[SwaggerOperation(description: $"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions")]
23+
[EndpointDescription(description: $"This endpoint requires the '{Permissions.PeopleRead}' or '{Permissions.PeopleAdmin}' permissions")]
2524
public IActionResult GetPerson(int id) => NoContent();
2625

2726
[Permission(Permissions.PeopleWrite)]
2827
[HttpPost]
2928
[ProducesResponseType(StatusCodes.Status204NoContent)]
3029
[ProducesDefaultResponseType]
31-
[SwaggerOperation(description: $"This endpoint requires the '{Permissions.PeopleWrite}' permission")]
30+
[EndpointDescription(description: $"This endpoint requires the '{Permissions.PeopleWrite}' permission")]
3231
public IActionResult Insert() => NoContent();
3332

3433
[Permission(Permissions.PeopleWrite)]
3534
[HttpPut]
3635
[ProducesResponseType(StatusCodes.Status204NoContent)]
3736
[ProducesDefaultResponseType]
38-
[SwaggerOperation(description: $"This endpoint requires the '{Permissions.PeopleWrite}' permission")]
37+
[EndpointDescription(description: $"This endpoint requires the '{Permissions.PeopleWrite}' permission")]
3938
public IActionResult Update() => NoContent();
4039

4140
[Permission(Permissions.PeopleAdmin)]
4241
[HttpDelete("{id:int}")]
4342
[ProducesResponseType(StatusCodes.Status204NoContent)]
4443
[ProducesDefaultResponseType]
45-
[SwaggerOperation(description: $"This endpoint requires the '{Permissions.PeopleAdmin}' permission")]
44+
[EndpointDescription(description: $"This endpoint requires the '{Permissions.PeopleAdmin}' permission")]
4645
public IActionResult Delete(int id) => NoContent();
4746
}
4847

samples/Controllers/JwtBearerSample/JwtBearerSample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
11-
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="9.0.3" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.4" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

samples/Controllers/JwtBearerSample/Program.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@
4141

4242
builder.Services.AddTransient<IClaimsTransformation, ClaimsTransformer>();
4343

44-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
45-
builder.Services.AddEndpointsApiExplorer();
46-
47-
builder.Services.AddSwaggerGen(options =>
44+
builder.Services.AddOpenApi(options =>
4845
{
49-
options.EnableAnnotations();
5046
options.AddSimpleAuthentication(builder.Configuration);
5147
});
5248

@@ -55,18 +51,15 @@
5551
// Configure the HTTP request pipeline.
5652
app.UseHttpsRedirection();
5753

58-
if (!app.Environment.IsDevelopment())
59-
{
60-
app.UseExceptionHandler();
61-
}
62-
54+
app.UseExceptionHandler();
6355
app.UseStatusCodePages();
6456

65-
if (app.Environment.IsDevelopment())
57+
app.MapOpenApi();
58+
59+
app.UseSwaggerUI(options =>
6660
{
67-
app.UseSwagger();
68-
app.UseSwaggerUI();
69-
}
61+
options.SwaggerEndpoint("/openapi/v1.json", app.Environment.ApplicationName);
62+
});
7063

7164
app.UseAuthentication();
7265
app.UseAuthorization();

samples/MinimalApis/ApiKeySample/ApiKeySample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.7" />
11-
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
11+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="9.0.4" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

0 commit comments

Comments
 (0)