Skip to content

Commit

Permalink
Remove IMvcCoreBuilder parameter from AddOpenApi()
Browse files Browse the repository at this point in the history
  • Loading branch information
bkoelman committed Feb 25, 2024
1 parent 7b139cb commit 0509424
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
7 changes: 2 additions & 5 deletions docs/usage/openapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppDbContext>(mvcBuilder: mvcCoreBuilder);
builder.Services.AddJsonApi<AppDbContext>();
// Configure Swashbuckle for JSON:API.
builder.Services.AddOpenApi(mvcCoreBuilder);
builder.Services.AddOpenApi();
var app = builder.Build();
Expand Down
6 changes: 2 additions & 4 deletions src/Examples/JsonApiDotNetCoreExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ static void ConfigureServices(WebApplicationBuilder builder)
SetDbContextDebugOptions(options);
});

IMvcCoreBuilder mvcCoreBuilder = builder.Services.AddMvcCore();

using (CodeTimingSessionManager.Current.Measure("AddJsonApi()"))
{
builder.Services.AddJsonApi<AppDbContext>(options =>
Expand All @@ -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<SetOpenApiServerAtBuildTimeFilter>());
builder.Services.AddOpenApi(options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ public static class ServiceCollectionExtensions
/// <summary>
/// Adds the OpenAPI integration to JsonApiDotNetCore by configuring Swashbuckle.
/// </summary>
public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder mvcBuilder, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
public static void AddOpenApi(this IServiceCollection services, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
{
ArgumentGuard.NotNull(services);
ArgumentGuard.NotNull(mvcBuilder);

AddCustomApiExplorer(services, mvcBuilder);
AddCustomApiExplorer(services);
AddCustomSwaggerComponents(services);
AddSwaggerGenerator(services);

Expand All @@ -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<OpenApiEndpointConvention>();
services.TryAddSingleton<JsonApiRequestFormatMetadataProvider>();
Expand All @@ -49,11 +48,19 @@ private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBu
return new ApiDescriptionGroupCollectionProvider(actionDescriptorCollectionProvider, apiDescriptionProviders);
}));

mvcBuilder.AddApiExplorer();
AddApiExplorer(services);

services.AddSingleton<IConfigureOptions<MvcOptions>, 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<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
}

private static void AddCustomSwaggerComponents(IServiceCollection services)
{
services.TryAddSingleton<ISerializerDataContractResolver, JsonApiDataContractResolver>();
Expand Down
6 changes: 2 additions & 4 deletions test/OpenApiTests/OpenApiStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ public class OpenApiStartup<TDbContext> : TestableStartup<TDbContext>
{
public override void ConfigureServices(IServiceCollection services)
{
IMvcCoreBuilder mvcBuilder = services.AddMvcCore();
base.ConfigureServices(services);

services.AddJsonApi<TDbContext>(SetJsonApiOptions, mvcBuilder: mvcBuilder);

services.AddOpenApi(mvcBuilder, SetupSwaggerGenAction);
services.AddOpenApi(SetupSwaggerGenAction);
}

protected override void SetJsonApiOptions(JsonApiOptions options)
Expand Down

0 comments on commit 0509424

Please sign in to comment.