Skip to content

Commit

Permalink
Use built-in OpenAPI support in templates (#55343)
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed May 10, 2024
1 parent 043e1d9 commit 785e994
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
MicrosoftNETCoreAppRuntimeVersion=$(MicrosoftNETCoreAppRuntimeVersion);
SystemNetHttpJsonVersion=$(SystemNetHttpJsonVersion);
MicrosoftGraphVersion=$(MicrosoftGraphVersion);
SwashbuckleAspNetCoreVersion=$(SwashbuckleAspNetCoreVersion);
</GeneratedContentProperties>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="${MicrosoftAspNetCoreAuthenticationJwtBearerVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="${MicrosoftAspNetCoreAuthenticationNegotiateVersion}" Condition="'$(WindowsAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="${MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="${MicrosoftAspNetCoreOpenApiVersion}" Condition="'$(EnableOpenAPI)' == 'True' AND '$(UsingMinimalAPIs)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="${MicrosoftAspNetCoreOpenApiVersion}" Condition="'$(EnableOpenAPI)' == 'True'" />
<PackageReference Include="Microsoft.Identity.Web" Version="${MicrosoftIdentityWebVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'"/>
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="${MicrosoftIdentityWebGraphServiceClientVersion}" Condition=" '$(GenerateGraph)' == 'True' " />
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="${MicrosoftIdentityWebDownstreamApiVersion}" Condition="'$(OrganizationalAuth)' == 'True' OR '$(IndividualB2CAuth)' == 'True'"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="${SwashbuckleAspNetCoreVersion}" Condition="'$(EnableOpenAPI)' == 'True'" />
</ItemGroup>

<!--#endif -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public static void Main(string[] args)
builder.Services.AddControllers();
#endif
#if (EnableOpenAPI)
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
#endif
#if (WindowsAuth)

Expand All @@ -81,8 +80,7 @@ public static void Main(string[] args)
#if (EnableOpenAPI)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}
#endif
#if (HasHttpsProfile)
Expand Down Expand Up @@ -165,12 +163,10 @@ public static void Main(string[] args)
#if (EnableOpenAPI && !NoAuth)
})
.WithName("GetWeatherForecast")
.WithOpenApi()
.RequireAuthorization();
#elif (EnableOpenAPI && NoAuth)
})
.WithName("GetWeatherForecast")
.WithOpenApi();
.WithName("GetWeatherForecast");
#elif (!EnableOpenAPI && !NoAuth)
})
.RequireAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
builder.Services.AddAuthorization();

#if (EnableOpenAPI)
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
#endif

var app = builder.Build();
Expand All @@ -53,8 +52,7 @@
#if (EnableOpenAPI)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}
#endif
#if (HasHttpsProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

// Add services to the container.
#if (EnableOpenAPI)
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
#endif
#if (WindowsAuth)
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
Expand All @@ -27,8 +26,7 @@
#if (EnableOpenAPI)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}
#endif
#if (HasHttpsProfile)
Expand All @@ -54,8 +52,7 @@
return forecast;
#if (EnableOpenAPI)
})
.WithName("GetWeatherForecast")
.WithOpenApi();
.WithName("GetWeatherForecast");
#else
});
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@

builder.Services.AddControllers();
#if (EnableOpenAPI)
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
#endif
#if (WindowsAuth)

Expand All @@ -68,8 +67,7 @@
#if (EnableOpenAPI)
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.MapOpenApi();
}
#endif
#if (HasHttpsProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
//#if (EnableOpenAPI)
"launchUrl": "swagger",
//#else
"launchUrl": "weatherforecast",
//#endif
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -39,11 +35,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
//#if (EnableOpenAPI)
"launchUrl": "swagger",
//#else
"launchUrl": "weatherforecast",
//#endif
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -53,11 +45,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
//#if (EnableOpenAPI)
"launchUrl": "swagger",
//#else
"launchUrl": "weatherforecast",
//#endif
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public async Task WebApiTemplateCSharp_WithoutOpenAPI(bool useProgramMain, bool
aspNetProcess.Process.HasExited,
ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));

await aspNetProcess.AssertNotFound("swagger");
await aspNetProcess.AssertNotFound("openapi/v1.json");
}

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

await aspNetProcess.AssertNotFound("swagger");
await aspNetProcess.AssertNotFound("openapi/v1.json");
}

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

await aspNetProcess.AssertOk("weatherforecast");
await aspNetProcess.AssertOk("swagger");
await aspNetProcess.AssertOk("openapi/v1.json");
await aspNetProcess.AssertNotFound("/");
}

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

await aspNetProcess.AssertOk("weatherforecast");
// Swagger is only available in Development
await aspNetProcess.AssertNotFound("swagger");
// OpenAPI endpoint is only enabled in Development
await aspNetProcess.AssertNotFound("openapi/v1.json");
await aspNetProcess.AssertNotFound("/");
}
}
Expand Down

0 comments on commit 785e994

Please sign in to comment.