From 2af293dc72625b865fa7fcad5aeb510df46386bd Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 25 Jun 2021 09:51:06 -0700 Subject: [PATCH] Added endpoint API explorer to default webhost (#33800) * Added endpoint API explorer to default webhost - This will allow discovery of API explorer metadata without an explicit call. - Added API explorer tests --- src/DefaultBuilder/DefaultBuilder.slnf | 8 +++++++- src/DefaultBuilder/src/Microsoft.AspNetCore.csproj | 1 + src/DefaultBuilder/src/WebHost.cs | 1 + .../WebApplicationTests.cs | 9 +++++++++ .../test/Microsoft.AspNetCore.Tests/WebHostTests.cs | 12 ++++++++++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/DefaultBuilder/DefaultBuilder.slnf b/src/DefaultBuilder/DefaultBuilder.slnf index 1f2d25e3a749..f91041934fa9 100644 --- a/src/DefaultBuilder/DefaultBuilder.slnf +++ b/src/DefaultBuilder/DefaultBuilder.slnf @@ -17,8 +17,14 @@ "src\\Http\\Http.Features\\src\\Microsoft.AspNetCore.Http.Features.csproj", "src\\Http\\Http\\src\\Microsoft.AspNetCore.Http.csproj", "src\\Http\\WebUtilities\\src\\Microsoft.AspNetCore.WebUtilities.csproj", + "src\\Middleware\\ResponseCaching.Abstractions\\src\\Microsoft.AspNetCore.ResponseCaching.Abstractions.csproj", "src\\Middleware\\StaticFiles\\src\\Microsoft.AspNetCore.StaticFiles.csproj", - "src\\ObjectPool\\src\\Microsoft.Extensions.ObjectPool.csproj" + "src\\Mvc\\Mvc.Abstractions\\src\\Microsoft.AspNetCore.Mvc.Abstractions.csproj", + "src\\Mvc\\Mvc.ApiExplorer\\src\\Microsoft.AspNetCore.Mvc.ApiExplorer.csproj", + "src\\Mvc\\Mvc.Core\\src\\Microsoft.AspNetCore.Mvc.Core.csproj", + "src\\ObjectPool\\src\\Microsoft.Extensions.ObjectPool.csproj", + "src\\Security\\Authentication\\Core\\src\\Microsoft.AspNetCore.Authentication.csproj", + "src\\Security\\Authorization\\Policy\\src\\Microsoft.AspNetCore.Authorization.Policy.csproj" ] } } \ No newline at end of file diff --git a/src/DefaultBuilder/src/Microsoft.AspNetCore.csproj b/src/DefaultBuilder/src/Microsoft.AspNetCore.csproj index 2c66a8407db3..5c25bdc36b74 100644 --- a/src/DefaultBuilder/src/Microsoft.AspNetCore.csproj +++ b/src/DefaultBuilder/src/Microsoft.AspNetCore.csproj @@ -18,6 +18,7 @@ + diff --git a/src/DefaultBuilder/src/WebHost.cs b/src/DefaultBuilder/src/WebHost.cs index 284c5a64a859..16f7d29c60c3 100644 --- a/src/DefaultBuilder/src/WebHost.cs +++ b/src/DefaultBuilder/src/WebHost.cs @@ -259,6 +259,7 @@ internal static void ConfigureWebDefaults(IWebHostBuilder builder) } services.AddRouting(); + services.AddEndpointsApiExplorer(); }) .UseIIS() .UseIISIntegration(); diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs index edd70f7568b8..622395bdcf40 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs @@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.Testing; @@ -269,6 +270,14 @@ public void WebApplicationCreate_RegistersRouting() Assert.NotNull(linkGenerator); } + [Fact] + public void WebApplicationCreate_RegistersApiExplorerForEndpoints() + { + var app = WebApplication.Create(); + var apiDescriptionProvider = app.Services.GetService(typeof(IApiDescriptionProvider)); + Assert.NotNull(apiDescriptionProvider); + } + [Fact] public void WebApplicationCreate_RegistersEventSourceLogger() { diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs index ec0d57a0e47e..c95cb25ec6ea 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs @@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HostFiltering; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.Testing; @@ -99,6 +100,17 @@ public void CreateDefaultBuilder_RegistersRouting() Assert.NotNull(linkGenerator); } + [Fact] + public void CreateDefaultBuilder_RegistersApiExplorer() + { + var host = WebHost.CreateDefaultBuilder() + .Configure(_ => { }) + .Build(); + + var apiDescriptionProvider = host.Services.GetService(typeof(IApiDescriptionProvider)); + Assert.NotNull(apiDescriptionProvider); + } + [Fact] public void CreateDefaultBuilder_RegistersEventSourceLogger() {