Skip to content

Commit daec9e7

Browse files
committed
Remove the need for a workaround for "dotnet swagger" (swashbuckle.aspnetcore.cli global tool)
1 parent 4d4d96f commit daec9e7

File tree

6 files changed

+46
-56
lines changed

6 files changed

+46
-56
lines changed
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,29 @@
11
using JsonApiDotNetCore.Configuration;
2-
using JsonApiDotNetCore.Middleware;
32
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.Extensions.Options;
54

65
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
76

87
internal sealed class ConfigureMvcOptions : IConfigureOptions<MvcOptions>
98
{
10-
private readonly IJsonApiRoutingConvention _jsonApiRoutingConvention;
119
private readonly JsonApiRequestFormatMetadataProvider _jsonApiRequestFormatMetadataProvider;
12-
private readonly IJsonApiOptions _jsonApiOptions;
10+
private readonly JsonApiOptions _jsonApiOptions;
1311

14-
public ConfigureMvcOptions(IJsonApiRoutingConvention jsonApiRoutingConvention, JsonApiRequestFormatMetadataProvider jsonApiRequestFormatMetadataProvider,
15-
IJsonApiOptions jsonApiOptions)
12+
public ConfigureMvcOptions(JsonApiRequestFormatMetadataProvider jsonApiRequestFormatMetadataProvider, IJsonApiOptions jsonApiOptions)
1613
{
17-
ArgumentNullException.ThrowIfNull(jsonApiRoutingConvention);
1814
ArgumentNullException.ThrowIfNull(jsonApiRequestFormatMetadataProvider);
1915
ArgumentNullException.ThrowIfNull(jsonApiOptions);
2016

21-
_jsonApiRoutingConvention = jsonApiRoutingConvention;
2217
_jsonApiRequestFormatMetadataProvider = jsonApiRequestFormatMetadataProvider;
23-
_jsonApiOptions = jsonApiOptions;
18+
_jsonApiOptions = (JsonApiOptions)jsonApiOptions;
2419
}
2520

2621
public void Configure(MvcOptions options)
2722
{
2823
ArgumentNullException.ThrowIfNull(options);
2924

30-
AddSwashbuckleCliCompatibility(options);
31-
3225
options.InputFormatters.Add(_jsonApiRequestFormatMetadataProvider);
3326

34-
((JsonApiOptions)_jsonApiOptions).IncludeExtensions(OpenApiMediaTypeExtension.OpenApi, OpenApiMediaTypeExtension.RelaxedOpenApi);
35-
}
36-
37-
private void AddSwashbuckleCliCompatibility(MvcOptions options)
38-
{
39-
if (!options.Conventions.Any(convention => convention is IJsonApiRoutingConvention))
40-
{
41-
// See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1957 for why this is needed.
42-
options.Conventions.Insert(0, _jsonApiRoutingConvention);
43-
}
27+
_jsonApiOptions.IncludeExtensions(OpenApiMediaTypeExtension.OpenApi, OpenApiMediaTypeExtension.RelaxedOpenApi);
4428
}
4529
}

src/JsonApiDotNetCore.OpenApi.Swashbuckle/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private static void AddCustomApiExplorer(IServiceCollection services)
6262

6363
AddApiExplorer(services);
6464

65-
services.AddSingleton<IConfigureOptions<MvcOptions>, ConfigureMvcOptions>();
65+
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<MvcOptions>, ConfigureMvcOptions>());
6666
}
6767

6868
private static void AddApiExplorer(IServiceCollection services)

src/JsonApiDotNetCore/Configuration/ApplicationBuilderExtensions.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,6 @@ public static void UseJsonApi(this IApplicationBuilder builder)
3333
inverseNavigationResolver.Resolve();
3434
}
3535

36-
var jsonApiApplicationBuilder = builder.ApplicationServices.GetRequiredService<IJsonApiApplicationBuilder>();
37-
38-
jsonApiApplicationBuilder.ConfigureMvcOptions = options =>
39-
{
40-
var inputFormatter = builder.ApplicationServices.GetRequiredService<IJsonApiInputFormatter>();
41-
options.InputFormatters.Insert(0, inputFormatter);
42-
43-
var outputFormatter = builder.ApplicationServices.GetRequiredService<IJsonApiOutputFormatter>();
44-
options.OutputFormatters.Insert(0, outputFormatter);
45-
46-
var routingConvention = builder.ApplicationServices.GetRequiredService<IJsonApiRoutingConvention>();
47-
options.Conventions.Insert(0, routingConvention);
48-
};
49-
5036
builder.UseMiddleware<JsonApiMiddleware>();
5137
}
5238

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using JsonApiDotNetCore.Middleware;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Options;
4+
5+
namespace JsonApiDotNetCore.Configuration;
6+
7+
internal sealed class ConfigureMvcOptions : IConfigureOptions<MvcOptions>
8+
{
9+
private readonly IJsonApiInputFormatter _inputFormatter;
10+
private readonly IJsonApiOutputFormatter _outputFormatter;
11+
private readonly IJsonApiRoutingConvention _routingConvention;
12+
13+
public ConfigureMvcOptions(IJsonApiInputFormatter inputFormatter, IJsonApiOutputFormatter outputFormatter, IJsonApiRoutingConvention routingConvention)
14+
{
15+
ArgumentNullException.ThrowIfNull(inputFormatter);
16+
ArgumentNullException.ThrowIfNull(outputFormatter);
17+
ArgumentNullException.ThrowIfNull(routingConvention);
18+
19+
_inputFormatter = inputFormatter;
20+
_outputFormatter = outputFormatter;
21+
_routingConvention = routingConvention;
22+
}
23+
24+
public void Configure(MvcOptions options)
25+
{
26+
ArgumentNullException.ThrowIfNull(options);
27+
28+
options.EnableEndpointRouting = true;
29+
30+
options.InputFormatters.Insert(0, _inputFormatter);
31+
options.OutputFormatters.Insert(0, _outputFormatter);
32+
options.Conventions.Insert(0, _routingConvention);
33+
34+
options.Filters.AddService<IAsyncJsonApiExceptionFilter>();
35+
options.Filters.AddService<IAsyncQueryStringActionFilter>();
36+
options.Filters.AddService<IAsyncConvertEmptyActionResultFilter>();
37+
}
38+
}

src/JsonApiDotNetCore/Configuration/IJsonApiApplicationBuilder.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/JsonApiDotNetCore/Configuration/JsonApiApplicationBuilder.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818
using Microsoft.Extensions.DependencyInjection;
1919
using Microsoft.Extensions.DependencyInjection.Extensions;
2020
using Microsoft.Extensions.Logging;
21+
using Microsoft.Extensions.Options;
2122

2223
namespace JsonApiDotNetCore.Configuration;
2324

2425
/// <summary>
2526
/// A utility class that builds a JSON:API application. It registers all required services and allows the user to override parts of the startup
2627
/// configuration.
2728
/// </summary>
28-
internal sealed class JsonApiApplicationBuilder : IJsonApiApplicationBuilder
29+
internal sealed class JsonApiApplicationBuilder
2930
{
3031
private readonly IServiceCollection _services;
3132
private readonly IMvcCoreBuilder _mvcBuilder;
3233
private readonly JsonApiOptions _options = new();
3334
private readonly ResourceDescriptorAssemblyCache _assemblyCache = new();
3435

35-
public Action<MvcOptions>? ConfigureMvcOptions { get; set; }
36-
3736
public JsonApiApplicationBuilder(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
3837
{
3938
ArgumentNullException.ThrowIfNull(services);
@@ -105,15 +104,6 @@ public void ConfigureResourceGraph(ICollection<Type> dbContextTypes, Action<Reso
105104
/// </summary>
106105
public void ConfigureMvc()
107106
{
108-
_mvcBuilder.AddMvcOptions(options =>
109-
{
110-
options.EnableEndpointRouting = true;
111-
options.Filters.AddService<IAsyncJsonApiExceptionFilter>();
112-
options.Filters.AddService<IAsyncQueryStringActionFilter>();
113-
options.Filters.AddService<IAsyncConvertEmptyActionResultFilter>();
114-
ConfigureMvcOptions?.Invoke(options);
115-
});
116-
117107
if (_options.ValidateModelState)
118108
{
119109
_mvcBuilder.AddDataAnnotations();
@@ -175,14 +165,14 @@ public void ConfigureServiceContainer(ICollection<Type> dbContextTypes)
175165
private void AddMiddlewareLayer()
176166
{
177167
_services.TryAddSingleton<IJsonApiOptions>(_options);
178-
_services.TryAddSingleton<IJsonApiApplicationBuilder>(this);
179168
_services.TryAddSingleton<IExceptionHandler, ExceptionHandler>();
180169
_services.TryAddScoped<IAsyncJsonApiExceptionFilter, AsyncJsonApiExceptionFilter>();
181170
_services.TryAddScoped<IAsyncQueryStringActionFilter, AsyncQueryStringActionFilter>();
182171
_services.TryAddScoped<IAsyncConvertEmptyActionResultFilter, AsyncConvertEmptyActionResultFilter>();
183172
_services.TryAddSingleton<IJsonApiInputFormatter, JsonApiInputFormatter>();
184173
_services.TryAddSingleton<IJsonApiOutputFormatter, JsonApiOutputFormatter>();
185174
_services.TryAddSingleton<IJsonApiRoutingConvention, JsonApiRoutingConvention>();
175+
_services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<MvcOptions>, ConfigureMvcOptions>());
186176
_services.TryAddSingleton<IControllerResourceMapping>(provider => provider.GetRequiredService<IJsonApiRoutingConvention>());
187177
_services.TryAddSingleton<IJsonApiEndpointFilter, AlwaysEnabledJsonApiEndpointFilter>();
188178
_services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

0 commit comments

Comments
 (0)