File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
src/JsonApiDotNetCore/Configuration Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 11using JsonApiDotNetCore . Middleware ;
22using Microsoft . AspNetCore . Builder ;
33using Microsoft . Extensions . DependencyInjection ;
4+ using Microsoft . Extensions . Options ;
45
56namespace JsonApiDotNetCore . Configuration ;
67
@@ -23,6 +24,7 @@ public static class ApplicationBuilderExtensions
2324 public static void UseJsonApi ( this IApplicationBuilder builder )
2425 {
2526 ArgumentNullException . ThrowIfNull ( builder ) ;
27+ AssertAspNetCoreOpenApiIsNotRegistered ( builder . ApplicationServices ) ;
2628
2729 using ( IServiceScope scope = builder . ApplicationServices . CreateScope ( ) )
2830 {
@@ -46,4 +48,22 @@ public static void UseJsonApi(this IApplicationBuilder builder)
4648
4749 builder . UseMiddleware < JsonApiMiddleware > ( ) ;
4850 }
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+ }
4969}
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 1414 <PackageReference Include =" coverlet.collector" Version =" $(CoverletVersion)" PrivateAssets =" All" />
1515 <PackageReference Include =" GitHubActionsTestLogger" Version =" $(GitHubActionsTestLoggerVersion)" PrivateAssets =" All" />
1616 <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" $(TestSdkVersion)" />
17+ <PackageReference Include =" Microsoft.AspNetCore.OpenApi" Condition =" '$(TargetFramework)' != 'net8.0'" Version =" $(AspNetCoreVersion)" />
1718 </ItemGroup >
1819</Project >
You can’t perform that action at this time.
0 commit comments