From eabe272b467539c72f0d4bb48e5155d05823bbcc Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 19 May 2021 21:24:00 +0200 Subject: [PATCH] Revert previous API Versioning config and instead expose ApiVersioningOptions to allow for UseApiBehavior to be disabled by host --- src/server/Elsa.Server.Api/ElsaApiOptions.cs | 23 ++----------------- .../Extensions/ServiceCollectionExtensions.cs | 20 ++++++++++++---- .../CompatibilityApiVersionConstraint.cs | 13 ----------- 3 files changed, 18 insertions(+), 38 deletions(-) delete mode 100644 src/server/Elsa.Server.Api/RouteConstraints/CompatibilityApiVersionConstraint.cs diff --git a/src/server/Elsa.Server.Api/ElsaApiOptions.cs b/src/server/Elsa.Server.Api/ElsaApiOptions.cs index dd14e9fea..2f543fc4f 100644 --- a/src/server/Elsa.Server.Api/ElsaApiOptions.cs +++ b/src/server/Elsa.Server.Api/ElsaApiOptions.cs @@ -1,31 +1,12 @@ using System; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Mvc.Versioning; namespace Elsa.Server.Api { public class ElsaApiOptions { - public static void AddDefaultApiVersioning(IServiceCollection services) - { - services.AddVersionedApiExplorer(o => - { - o.GroupNameFormat = "'v'VVV"; - o.SubstituteApiVersionInUrl = true; - }); - - services.AddApiVersioning( - options => - { - options.ReportApiVersions = true; - options.DefaultApiVersion = ApiVersion.Default; - options.AssumeDefaultVersionWhenUnspecified = true; - }); - } - public Action? SetupNewtonsoftJson { get; set; } = default; - public Action? SetupApiVersioning { get; set; } = AddDefaultApiVersioning; - - public void DisableApiVersioning() => SetupApiVersioning = null; + public Action? ConfigureApiVersioningOptions { get; set; } } } \ No newline at end of file diff --git a/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs b/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs index 3117fd2dc..2012b8be5 100644 --- a/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs +++ b/src/server/Elsa.Server.Api/Extensions/ServiceCollectionExtensions.cs @@ -3,9 +3,9 @@ using Elsa; using Elsa.Models; using Elsa.Server.Api; using Elsa.Server.Api.Mapping; -using Elsa.Server.Api.RouteConstraints; using Elsa.Server.Api.Services; using Elsa.Server.Api.Swagger.Examples; +using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.Filters; @@ -19,8 +19,8 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddElsaApiEndpoints(this IServiceCollection services, ElsaApiOptions apiOptions) => services.AddElsaApiEndpoints(options => { - options.SetupApiVersioning = apiOptions.SetupApiVersioning; options.SetupNewtonsoftJson = apiOptions.SetupNewtonsoftJson; + options.ConfigureApiVersioningOptions = apiOptions.ConfigureApiVersioningOptions; }); public static IServiceCollection AddElsaApiEndpoints(this IServiceCollection services, Action? configureApiOptions = default) @@ -33,9 +33,21 @@ namespace Microsoft.Extensions.DependencyInjection services.AddControllers().AddNewtonsoftJson(setupNewtonsoftJson); services.AddRouting(options => { options.LowercaseUrls = true; }); - var addApiVersioning = apiOptions.SetupApiVersioning ?? (_ => { services.AddRouting(routeOptions => routeOptions.ConstraintMap["apiVersion"] = typeof(CompatibilityApiVersionConstraint)); }); + services.AddVersionedApiExplorer(o => + { + o.GroupNameFormat = "'v'VVV"; + o.SubstituteApiVersionInUrl = true; + }); + + services.AddApiVersioning( + options => + { + options.ReportApiVersions = true; + options.DefaultApiVersion = ApiVersion.Default; + options.AssumeDefaultVersionWhenUnspecified = true; + apiOptions.ConfigureApiVersioningOptions?.Invoke(options); + }); - addApiVersioning(services); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/server/Elsa.Server.Api/RouteConstraints/CompatibilityApiVersionConstraint.cs b/src/server/Elsa.Server.Api/RouteConstraints/CompatibilityApiVersionConstraint.cs deleted file mode 100644 index 97687b749..000000000 --- a/src/server/Elsa.Server.Api/RouteConstraints/CompatibilityApiVersionConstraint.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Routing; - -namespace Elsa.Server.Api.RouteConstraints -{ - /// - /// In case the hosting app wishes to opt-out of API Versioning, we still need to handle the "apiVersion" constraint. - /// - public class CompatibilityApiVersionConstraint : IRouteConstraint - { - public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) => true; - } -} \ No newline at end of file