Revert previous API Versioning config and instead expose ApiVersioningOptions to allow for UseApiBehavior to be disabled by host
This commit is contained in:
parent
a6510b242a
commit
eabe272b46
|
|
@ -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<MvcNewtonsoftJsonOptions>? SetupNewtonsoftJson { get; set; } = default;
|
||||
public Action<IServiceCollection>? SetupApiVersioning { get; set; } = AddDefaultApiVersioning;
|
||||
|
||||
public void DisableApiVersioning() => SetupApiVersioning = null;
|
||||
public Action<ApiVersioningOptions>? ConfigureApiVersioningOptions { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ElsaApiOptions>? 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<ConnectionConverter>();
|
||||
services.AddSingleton<ActivityBlueprintConverter>();
|
||||
services.AddSingleton<IWorkflowBlueprintMapper, WorkflowBlueprintMapper>();
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Elsa.Server.Api.RouteConstraints
|
||||
{
|
||||
/// <summary>
|
||||
/// In case the hosting app wishes to opt-out of API Versioning, we still need to handle the "apiVersion" constraint.
|
||||
/// </summary>
|
||||
public class CompatibilityApiVersionConstraint : IRouteConstraint
|
||||
{
|
||||
public bool Match(HttpContext? httpContext, IRouter? route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) => true;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue