From 05094247f8b59e476900c00ceed5f3d16379f420 Mon Sep 17 00:00:00 2001 From: Bart Koelman <10324372+bkoelman@users.noreply.github.com> Date: Sun, 25 Feb 2024 14:21:32 +0100 Subject: [PATCH] Remove IMvcCoreBuilder parameter from AddOpenApi() --- docs/usage/openapi.md | 7 ++----- .../JsonApiDotNetCoreExample/Program.cs | 6 ++---- .../ServiceCollectionExtensions.cs | 17 ++++++++++++----- test/OpenApiTests/OpenApiStartup.cs | 6 ++---- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/usage/openapi.md b/docs/usage/openapi.md index 1419a6ba43..e26754d99b 100644 --- a/docs/usage/openapi.md +++ b/docs/usage/openapi.md @@ -16,13 +16,10 @@ The package provides an integration with [Swashbuckle](https://github.com/domain 2. Add the integration in your `Program.cs` file. ```c# - IMvcCoreBuilder mvcCoreBuilder = builder.Services.AddMvcCore(); - - // Include the mvcBuilder parameter. - builder.Services.AddJsonApi(mvcBuilder: mvcCoreBuilder); + builder.Services.AddJsonApi(); // Configure Swashbuckle for JSON:API. - builder.Services.AddOpenApi(mvcCoreBuilder); + builder.Services.AddOpenApi(); var app = builder.Build(); diff --git a/src/Examples/JsonApiDotNetCoreExample/Program.cs b/src/Examples/JsonApiDotNetCoreExample/Program.cs index 332235d491..925c2f02e3 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Program.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Program.cs @@ -66,8 +66,6 @@ static void ConfigureServices(WebApplicationBuilder builder) SetDbContextDebugOptions(options); }); - IMvcCoreBuilder mvcCoreBuilder = builder.Services.AddMvcCore(); - using (CodeTimingSessionManager.Current.Measure("AddJsonApi()")) { builder.Services.AddJsonApi(options => @@ -82,12 +80,12 @@ static void ConfigureServices(WebApplicationBuilder builder) options.IncludeRequestBodyInErrors = true; options.SerializerOptions.WriteIndented = true; #endif - }, discovery => discovery.AddCurrentAssembly(), mvcBuilder: mvcCoreBuilder); + }, discovery => discovery.AddCurrentAssembly()); } using (CodeTimingSessionManager.Current.Measure("AddOpenApi()")) { - builder.Services.AddOpenApi(mvcCoreBuilder, options => options.DocumentFilter()); + builder.Services.AddOpenApi(options => options.DocumentFilter()); } } diff --git a/src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs b/src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs index cc9c0f179c..ad976b6fc7 100644 --- a/src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs +++ b/src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs @@ -15,12 +15,11 @@ public static class ServiceCollectionExtensions /// /// Adds the OpenAPI integration to JsonApiDotNetCore by configuring Swashbuckle. /// - public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder mvcBuilder, Action? setupSwaggerGenAction = null) + public static void AddOpenApi(this IServiceCollection services, Action? setupSwaggerGenAction = null) { ArgumentGuard.NotNull(services); - ArgumentGuard.NotNull(mvcBuilder); - AddCustomApiExplorer(services, mvcBuilder); + AddCustomApiExplorer(services); AddCustomSwaggerComponents(services); AddSwaggerGenerator(services); @@ -30,7 +29,7 @@ public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder } } - private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBuilder mvcBuilder) + private static void AddCustomApiExplorer(IServiceCollection services) { services.TryAddSingleton(); services.TryAddSingleton(); @@ -49,11 +48,19 @@ private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBu return new ApiDescriptionGroupCollectionProvider(actionDescriptorCollectionProvider, apiDescriptionProviders); })); - mvcBuilder.AddApiExplorer(); + AddApiExplorer(services); services.AddSingleton, ConfigureMvcOptions>(); } + private static void AddApiExplorer(IServiceCollection services) + { + // The code below was copied from the implementation of MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(), + // so we don't need to take IMvcCoreBuilder as an input parameter. + + services.TryAddEnumerable(ServiceDescriptor.Transient()); + } + private static void AddCustomSwaggerComponents(IServiceCollection services) { services.TryAddSingleton(); diff --git a/test/OpenApiTests/OpenApiStartup.cs b/test/OpenApiTests/OpenApiStartup.cs index e46efe63f0..86e964cc49 100644 --- a/test/OpenApiTests/OpenApiStartup.cs +++ b/test/OpenApiTests/OpenApiStartup.cs @@ -13,11 +13,9 @@ public class OpenApiStartup : TestableStartup { public override void ConfigureServices(IServiceCollection services) { - IMvcCoreBuilder mvcBuilder = services.AddMvcCore(); + base.ConfigureServices(services); - services.AddJsonApi(SetJsonApiOptions, mvcBuilder: mvcBuilder); - - services.AddOpenApi(mvcBuilder, SetupSwaggerGenAction); + services.AddOpenApi(SetupSwaggerGenAction); } protected override void SetJsonApiOptions(JsonApiOptions options)