File tree 3 files changed +49
-0
lines changed
src/JsonApiDotNetCore/Configuration
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1
1
using JsonApiDotNetCore . Middleware ;
2
2
using Microsoft . AspNetCore . Builder ;
3
3
using Microsoft . Extensions . DependencyInjection ;
4
+ using Microsoft . Extensions . Options ;
4
5
5
6
namespace JsonApiDotNetCore . Configuration ;
6
7
@@ -23,6 +24,7 @@ public static class ApplicationBuilderExtensions
23
24
public static void UseJsonApi ( this IApplicationBuilder builder )
24
25
{
25
26
ArgumentNullException . ThrowIfNull ( builder ) ;
27
+ AssertAspNetCoreOpenApiIsNotRegistered ( builder . ApplicationServices ) ;
26
28
27
29
using ( IServiceScope scope = builder . ApplicationServices . CreateScope ( ) )
28
30
{
@@ -46,4 +48,22 @@ public static void UseJsonApi(this IApplicationBuilder builder)
46
48
47
49
builder . UseMiddleware < JsonApiMiddleware > ( ) ;
48
50
}
51
+
52
+ private static void AssertAspNetCoreOpenApiIsNotRegistered ( IServiceProvider serviceProvider )
53
+ {
54
+ var optionsType = Type . GetType ( "Microsoft.AspNetCore.OpenApi.OpenApiOptions, Microsoft.AspNetCore.OpenApi" ) ;
55
+
56
+ if ( optionsType != null )
57
+ {
58
+ Type configureType = typeof ( IConfigureOptions < > ) . MakeGenericType ( optionsType ) ;
59
+
60
+ object ? instance = serviceProvider . GetService ( configureType ) ;
61
+
62
+ if ( instance != null )
63
+ {
64
+ throw new InvalidOperationException ( "JsonApiDotNetCore is incompatible with ASP.NET OpenAPI. " +
65
+ "Replace 'services.AddOpenApi()' with 'services.AddOpenApiForJsonApi()' from the JsonApiDotNetCore.OpenApi.Swashbuckle NuGet package." ) ;
66
+ }
67
+ }
68
+ }
49
69
}
Original file line number Diff line number Diff line change
1
+ #if ! NET8_0
2
+ using FluentAssertions ;
3
+ using JsonApiDotNetCore . Configuration ;
4
+ using Microsoft . AspNetCore . Builder ;
5
+ using Microsoft . AspNetCore . TestHost ;
6
+ using Microsoft . Extensions . DependencyInjection ;
7
+ using Xunit ;
8
+
9
+ namespace DiscoveryTests ;
10
+
11
+ public sealed class AspNetOpenApiTests
12
+ {
13
+ [ Fact ]
14
+ public async Task Throws_when_AspNet_OpenApi_is_registered ( )
15
+ {
16
+ WebApplicationBuilder builder = WebApplication . CreateEmptyBuilder ( new WebApplicationOptions ( ) ) ;
17
+ builder . WebHost . UseTestServer ( ) ;
18
+ builder . Services . AddJsonApi ( ) ;
19
+ builder . Services . AddOpenApi ( ) ;
20
+
21
+ await using WebApplication app = builder . Build ( ) ;
22
+ Action action = app . UseJsonApi ;
23
+
24
+ action . Should ( ) . ThrowExactly < InvalidOperationException > ( ) . WithMessage ( "JsonApiDotNetCore is incompatible with ASP.NET OpenAPI. " +
25
+ "Replace 'services.AddOpenApi()' with 'services.AddOpenApiForJsonApi()' from the JsonApiDotNetCore.OpenApi.Swashbuckle NuGet package." ) ;
26
+ }
27
+ }
28
+ #endif
Original file line number Diff line number Diff line change 14
14
<PackageReference Include =" coverlet.collector" Version =" $(CoverletVersion)" PrivateAssets =" All" />
15
15
<PackageReference Include =" GitHubActionsTestLogger" Version =" $(GitHubActionsTestLoggerVersion)" PrivateAssets =" All" />
16
16
<PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" $(TestSdkVersion)" />
17
+ <PackageReference Include =" Microsoft.AspNetCore.OpenApi" Condition =" '$(TargetFramework)' != 'net8.0'" Version =" $(AspNetCoreVersion)" />
17
18
</ItemGroup >
18
19
</Project >
You can’t perform that action at this time.
0 commit comments