Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use built-in OpenAPI support in templates #55343

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
// OpenAPI endpoint is only enabled in Development
await aspNetProcess.AssertNotFound("openapi/v1.json");
await aspNetProcess.AssertNotFound("/");
}
}
Expand Down
Loading