Skip to content

Commit 0509424

Browse files
committed
Remove IMvcCoreBuilder parameter from AddOpenApi()
1 parent 7b139cb commit 0509424

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

docs/usage/openapi.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ The package provides an integration with [Swashbuckle](https://github.com/domain
1616
2. Add the integration in your `Program.cs` file.
1717
1818
```c#
19-
IMvcCoreBuilder mvcCoreBuilder = builder.Services.AddMvcCore();
20-
21-
// Include the mvcBuilder parameter.
22-
builder.Services.AddJsonApi<AppDbContext>(mvcBuilder: mvcCoreBuilder);
19+
builder.Services.AddJsonApi<AppDbContext>();
2320
2421
// Configure Swashbuckle for JSON:API.
25-
builder.Services.AddOpenApi(mvcCoreBuilder);
22+
builder.Services.AddOpenApi();
2623
2724
var app = builder.Build();
2825

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ static void ConfigureServices(WebApplicationBuilder builder)
6666
SetDbContextDebugOptions(options);
6767
});
6868

69-
IMvcCoreBuilder mvcCoreBuilder = builder.Services.AddMvcCore();
70-
7169
using (CodeTimingSessionManager.Current.Measure("AddJsonApi()"))
7270
{
7371
builder.Services.AddJsonApi<AppDbContext>(options =>
@@ -82,12 +80,12 @@ static void ConfigureServices(WebApplicationBuilder builder)
8280
options.IncludeRequestBodyInErrors = true;
8381
options.SerializerOptions.WriteIndented = true;
8482
#endif
85-
}, discovery => discovery.AddCurrentAssembly(), mvcBuilder: mvcCoreBuilder);
83+
}, discovery => discovery.AddCurrentAssembly());
8684
}
8785

8886
using (CodeTimingSessionManager.Current.Measure("AddOpenApi()"))
8987
{
90-
builder.Services.AddOpenApi(mvcCoreBuilder, options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
88+
builder.Services.AddOpenApi(options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
9189
}
9290
}
9391

src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ public static class ServiceCollectionExtensions
1515
/// <summary>
1616
/// Adds the OpenAPI integration to JsonApiDotNetCore by configuring Swashbuckle.
1717
/// </summary>
18-
public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder mvcBuilder, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
18+
public static void AddOpenApi(this IServiceCollection services, Action<SwaggerGenOptions>? setupSwaggerGenAction = null)
1919
{
2020
ArgumentGuard.NotNull(services);
21-
ArgumentGuard.NotNull(mvcBuilder);
2221

23-
AddCustomApiExplorer(services, mvcBuilder);
22+
AddCustomApiExplorer(services);
2423
AddCustomSwaggerComponents(services);
2524
AddSwaggerGenerator(services);
2625

@@ -30,7 +29,7 @@ public static void AddOpenApi(this IServiceCollection services, IMvcCoreBuilder
3029
}
3130
}
3231

33-
private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
32+
private static void AddCustomApiExplorer(IServiceCollection services)
3433
{
3534
services.TryAddSingleton<OpenApiEndpointConvention>();
3635
services.TryAddSingleton<JsonApiRequestFormatMetadataProvider>();
@@ -49,11 +48,19 @@ private static void AddCustomApiExplorer(IServiceCollection services, IMvcCoreBu
4948
return new ApiDescriptionGroupCollectionProvider(actionDescriptorCollectionProvider, apiDescriptionProviders);
5049
}));
5150

52-
mvcBuilder.AddApiExplorer();
51+
AddApiExplorer(services);
5352

5453
services.AddSingleton<IConfigureOptions<MvcOptions>, ConfigureMvcOptions>();
5554
}
5655

56+
private static void AddApiExplorer(IServiceCollection services)
57+
{
58+
// The code below was copied from the implementation of MvcApiExplorerMvcCoreBuilderExtensions.AddApiExplorer(),
59+
// so we don't need to take IMvcCoreBuilder as an input parameter.
60+
61+
services.TryAddEnumerable(ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
62+
}
63+
5764
private static void AddCustomSwaggerComponents(IServiceCollection services)
5865
{
5966
services.TryAddSingleton<ISerializerDataContractResolver, JsonApiDataContractResolver>();

test/OpenApiTests/OpenApiStartup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ public class OpenApiStartup<TDbContext> : TestableStartup<TDbContext>
1313
{
1414
public override void ConfigureServices(IServiceCollection services)
1515
{
16-
IMvcCoreBuilder mvcBuilder = services.AddMvcCore();
16+
base.ConfigureServices(services);
1717

18-
services.AddJsonApi<TDbContext>(SetJsonApiOptions, mvcBuilder: mvcBuilder);
19-
20-
services.AddOpenApi(mvcBuilder, SetupSwaggerGenAction);
18+
services.AddOpenApi(SetupSwaggerGenAction);
2119
}
2220

2321
protected override void SetJsonApiOptions(JsonApiOptions options)

0 commit comments

Comments
 (0)