Skip to content

Commit 785e994

Browse files
authored
Use built-in OpenAPI support in templates (#55343)
1 parent 043e1d9 commit 785e994

File tree

8 files changed

+20
-45
lines changed

8 files changed

+20
-45
lines changed

src/ProjectTemplates/Web.ProjectTemplates/Microsoft.DotNet.Web.ProjectTemplates.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
MicrosoftNETCoreAppRuntimeVersion=$(MicrosoftNETCoreAppRuntimeVersion);
2727
SystemNetHttpJsonVersion=$(SystemNetHttpJsonVersion);
2828
MicrosoftGraphVersion=$(MicrosoftGraphVersion);
29-
SwashbuckleAspNetCoreVersion=$(SwashbuckleAspNetCoreVersion);
3029
</GeneratedContentProperties>
3130
</PropertyGroup>
3231

src/ProjectTemplates/Web.ProjectTemplates/WebApi-CSharp.csproj.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="${MicrosoftAspNetCoreAuthenticationJwtBearerVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'" NoWarn="NU1605" />
1515
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="${MicrosoftAspNetCoreAuthenticationNegotiateVersion}" Condition="'$(WindowsAuth)' == 'True'" />
1616
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="${MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'" NoWarn="NU1605" />
17-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="${MicrosoftAspNetCoreOpenApiVersion}" Condition="'$(EnableOpenAPI)' == 'True' AND '$(UsingMinimalAPIs)' == 'True'" />
17+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="${MicrosoftAspNetCoreOpenApiVersion}" Condition="'$(EnableOpenAPI)' == 'True'" />
1818
<PackageReference Include="Microsoft.Identity.Web" Version="${MicrosoftIdentityWebVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'"/>
1919
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="${MicrosoftIdentityWebGraphServiceClientVersion}" Condition=" '$(GenerateGraph)' == 'True' " />
2020
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="${MicrosoftIdentityWebDownstreamApiVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'"/>
21-
<PackageReference Include="Swashbuckle.AspNetCore" Version="${SwashbuckleAspNetCoreVersion}" Condition="'$(EnableOpenAPI)' == 'True'" />
2221
</ItemGroup>
2322

2423
<!--#endif -->

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Program.Main.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ public static void Main(string[] args)
5959
builder.Services.AddControllers();
6060
#endif
6161
#if (EnableOpenAPI)
62-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
63-
builder.Services.AddEndpointsApiExplorer();
64-
builder.Services.AddSwaggerGen();
62+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
63+
builder.Services.AddOpenApi();
6564
#endif
6665
#if (WindowsAuth)
6766

@@ -81,8 +80,7 @@ public static void Main(string[] args)
8180
#if (EnableOpenAPI)
8281
if (app.Environment.IsDevelopment())
8382
{
84-
app.UseSwagger();
85-
app.UseSwaggerUI();
83+
app.MapOpenApi();
8684
}
8785
#endif
8886
#if (HasHttpsProfile)
@@ -165,12 +163,10 @@ public static void Main(string[] args)
165163
#if (EnableOpenAPI && !NoAuth)
166164
})
167165
.WithName("GetWeatherForecast")
168-
.WithOpenApi()
169166
.RequireAuthorization();
170167
#elif (EnableOpenAPI && NoAuth)
171168
})
172-
.WithName("GetWeatherForecast")
173-
.WithOpenApi();
169+
.WithName("GetWeatherForecast");
174170
#elif (!EnableOpenAPI && !NoAuth)
175171
})
176172
.RequireAuthorization();

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Program.MinimalAPIs.OrgOrIndividualB2CAuth.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@
4242
builder.Services.AddAuthorization();
4343

4444
#if (EnableOpenAPI)
45-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
46-
builder.Services.AddEndpointsApiExplorer();
47-
builder.Services.AddSwaggerGen();
45+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
46+
builder.Services.AddOpenApi();
4847
#endif
4948

5049
var app = builder.Build();
@@ -53,8 +52,7 @@
5352
#if (EnableOpenAPI)
5453
if (app.Environment.IsDevelopment())
5554
{
56-
app.UseSwagger();
57-
app.UseSwaggerUI();
55+
app.MapOpenApi();
5856
}
5957
#endif
6058
#if (HasHttpsProfile)

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Program.MinimalAPIs.WindowsOrNoAuth.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
// Add services to the container.
88
#if (EnableOpenAPI)
9-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
10-
builder.Services.AddEndpointsApiExplorer();
11-
builder.Services.AddSwaggerGen();
9+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
10+
builder.Services.AddOpenApi();
1211
#endif
1312
#if (WindowsAuth)
1413
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
@@ -27,8 +26,7 @@
2726
#if (EnableOpenAPI)
2827
if (app.Environment.IsDevelopment())
2928
{
30-
app.UseSwagger();
31-
app.UseSwaggerUI();
29+
app.MapOpenApi();
3230
}
3331
#endif
3432
#if (HasHttpsProfile)
@@ -54,8 +52,7 @@
5452
return forecast;
5553
#if (EnableOpenAPI)
5654
})
57-
.WithName("GetWeatherForecast")
58-
.WithOpenApi();
55+
.WithName("GetWeatherForecast");
5956
#else
6057
});
6158
#endif

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Program.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@
4646

4747
builder.Services.AddControllers();
4848
#if (EnableOpenAPI)
49-
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
50-
builder.Services.AddEndpointsApiExplorer();
51-
builder.Services.AddSwaggerGen();
49+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
50+
builder.Services.AddOpenApi();
5251
#endif
5352
#if (WindowsAuth)
5453

@@ -68,8 +67,7 @@
6867
#if (EnableOpenAPI)
6968
if (app.Environment.IsDevelopment())
7069
{
71-
app.UseSwagger();
72-
app.UseSwaggerUI();
70+
app.MapOpenApi();
7371
}
7472
#endif
7573
#if (HasHttpsProfile)

src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Properties/launchSettings.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
"commandName": "Project",
2424
"dotnetRunMessages": true,
2525
"launchBrowser": true,
26-
//#if (EnableOpenAPI)
27-
"launchUrl": "swagger",
28-
//#else
2926
"launchUrl": "weatherforecast",
30-
//#endif
3127
"applicationUrl": "http://localhost:5000",
3228
"environmentVariables": {
3329
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -39,11 +35,7 @@
3935
"commandName": "Project",
4036
"dotnetRunMessages": true,
4137
"launchBrowser": true,
42-
//#if (EnableOpenAPI)
43-
"launchUrl": "swagger",
44-
//#else
4538
"launchUrl": "weatherforecast",
46-
//#endif
4739
"applicationUrl": "https://localhost:5001;http://localhost:5000",
4840
"environmentVariables": {
4941
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -53,11 +45,7 @@
5345
"IIS Express": {
5446
"commandName": "IISExpress",
5547
"launchBrowser": true,
56-
//#if (EnableOpenAPI)
57-
"launchUrl": "swagger",
58-
//#else
5948
"launchUrl": "weatherforecast",
60-
//#endif
6149
"environmentVariables": {
6250
"ASPNETCORE_ENVIRONMENT": "Development"
6351
}

src/ProjectTemplates/test/Templates.Mvc.Tests/WebApiTemplateTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public async Task WebApiTemplateCSharp_WithoutOpenAPI(bool useProgramMain, bool
171171
aspNetProcess.Process.HasExited,
172172
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
173173

174-
await aspNetProcess.AssertNotFound("swagger");
174+
await aspNetProcess.AssertNotFound("openapi/v1.json");
175175
}
176176

177177
[ConditionalTheory]
@@ -206,7 +206,7 @@ public async Task WebApiTemplateCSharpNoHttps_WithoutOpenAPI(bool useProgramMain
206206
aspNetProcess.Process.HasExited,
207207
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
208208

209-
await aspNetProcess.AssertNotFound("swagger");
209+
await aspNetProcess.AssertNotFound("openapi/v1.json");
210210
}
211211

212212
private async Task<Project> PublishAndBuildWebApiTemplate(string languageOverride, string auth, string[] args = null)
@@ -260,7 +260,7 @@ private async Task WebApiTemplateCore(string languageOverride, string[] args = n
260260
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
261261

262262
await aspNetProcess.AssertOk("weatherforecast");
263-
await aspNetProcess.AssertOk("swagger");
263+
await aspNetProcess.AssertOk("openapi/v1.json");
264264
await aspNetProcess.AssertNotFound("/");
265265
}
266266

@@ -271,8 +271,8 @@ private async Task WebApiTemplateCore(string languageOverride, string[] args = n
271271
ErrorMessages.GetFailedProcessMessageOrEmpty("Run published project", project, aspNetProcess.Process));
272272

273273
await aspNetProcess.AssertOk("weatherforecast");
274-
// Swagger is only available in Development
275-
await aspNetProcess.AssertNotFound("swagger");
274+
// OpenAPI endpoint is only enabled in Development
275+
await aspNetProcess.AssertNotFound("openapi/v1.json");
276276
await aspNetProcess.AssertNotFound("/");
277277
}
278278
}

0 commit comments

Comments
 (0)