Skip to content

Commit 74059a6

Browse files
authored
Throw at startup when different OpenAPI version is loaded (#1819)
1 parent 7b1cc3a commit 74059a6

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/JsonApiDotNetCore/Configuration/ServiceCollectionExtensions.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using JetBrains.Annotations;
23
using JsonApiDotNetCore.Errors;
34
using 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

Comments
 (0)