1+ using System . Reflection ;
12using JetBrains . Annotations ;
23using JsonApiDotNetCore . Errors ;
34using JsonApiDotNetCore . Repositories ;
@@ -13,6 +14,16 @@ public static class ServiceCollectionExtensions
1314{
1415 private static readonly TypeLocator TypeLocator = new ( ) ;
1516
17+ /// <summary>
18+ /// Configures JsonApiDotNetCore by registering resources from an Entity Framework Core model.
19+ /// </summary>
20+ public static IServiceCollection AddJsonApi < TDbContext > ( this IServiceCollection services , Action < JsonApiOptions > ? options = null ,
21+ Action < ServiceDiscoveryFacade > ? discovery = null , Action < ResourceGraphBuilder > ? resources = null , IMvcCoreBuilder ? mvcBuilder = null )
22+ where TDbContext : DbContext
23+ {
24+ return AddJsonApi ( services , options , discovery , resources , mvcBuilder , [ typeof ( TDbContext ) ] ) ;
25+ }
26+
1627 /// <summary>
1728 /// Configures JsonApiDotNetCore by registering resources manually.
1829 /// </summary>
@@ -23,20 +34,33 @@ public static IServiceCollection AddJsonApi(this IServiceCollection services, Ac
2334#pragma warning restore AV1553 // Do not use optional parameters with default value null for strings, collections or tasks
2435 {
2536 ArgumentNullException . ThrowIfNull ( services ) ;
37+ AssertCompatibleOpenApiVersion ( ) ;
2638
2739 SetupApplicationBuilder ( services , options , discovery , resources , mvcBuilder , dbContextTypes ?? Array . Empty < Type > ( ) ) ;
2840
2941 return services ;
3042 }
3143
32- /// <summary>
33- /// Configures JsonApiDotNetCore by registering resources from an Entity Framework Core model.
34- /// </summary>
35- public static IServiceCollection AddJsonApi < TDbContext > ( this IServiceCollection services , Action < JsonApiOptions > ? options = null ,
36- Action < ServiceDiscoveryFacade > ? discovery = null , Action < ResourceGraphBuilder > ? resources = null , IMvcCoreBuilder ? mvcBuilder = null )
37- where TDbContext : DbContext
44+ private static void AssertCompatibleOpenApiVersion ( )
3845 {
39- return AddJsonApi ( services , options , discovery , resources , mvcBuilder , [ typeof ( TDbContext ) ] ) ;
46+ Version thisAssemblyVersion = typeof ( IJsonApiOptions ) . Assembly . GetName ( ) . Version ! ;
47+ Version ? openApiAssemblyVersion = TryGetOpenApiAssemblyVersion ( ) ;
48+
49+ if ( openApiAssemblyVersion != null && openApiAssemblyVersion != thisAssemblyVersion )
50+ {
51+ throw new InvalidOperationException (
52+ $ "JsonApiDotNetCore v{ thisAssemblyVersion . ToString ( 3 ) } is incompatible with JsonApiDotNetCore.OpenApi.Swashbuckle v{ openApiAssemblyVersion . ToString ( 3 ) } . " +
53+ $ "Reference a matching (preview) version of the JsonApiDotNetCore.OpenApi.Swashbuckle NuGet package.") ;
54+ }
55+ }
56+
57+ private static Version ? TryGetOpenApiAssemblyVersion ( )
58+ {
59+ Assembly ? openApiAssembly = AppDomain . CurrentDomain . GetAssemblies ( ) . FirstOrDefault ( assembly =>
60+ assembly . FullName ? . StartsWith ( "JsonApiDotNetCore.OpenApi.Swashbuckle" , StringComparison . Ordinal ) == true &&
61+ assembly . GetName ( ) . Name == "JsonApiDotNetCore.OpenApi.Swashbuckle" ) ;
62+
63+ return openApiAssembly ? . GetType ( "JsonApiDotNetCore.OpenApi.Swashbuckle.ServiceCollectionExtensions" , false ) ? . Assembly . GetName ( ) . Version ;
4064 }
4165
4266 private static void SetupApplicationBuilder ( IServiceCollection services , Action < JsonApiOptions > ? configureOptions ,
0 commit comments