Skip to content

Commit

Permalink
Deduplicate the magic string "/management/api/" (#16016)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjac authored Apr 10, 2024
1 parent 7b6db02 commit 1c79980
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Umbraco.Cms.Api.Common/Security/Paths.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Umbraco.Cms.Core;

namespace Umbraco.Cms.Api.Common.Security;

public static class Paths
Expand All @@ -14,7 +16,7 @@ public static class BackOfficeApi

public static readonly string RevokeEndpoint = EndpointPath($"{EndpointTemplate}/revoke");

private static string EndpointPath(string relativePath) => $"/umbraco/management/api/v1/{relativePath}";
private static string EndpointPath(string relativePath) => $"/umbraco{Constants.Web.ManagementApiPath}v1/{relativePath}";
}

public static class MemberApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ internal static IApplicationBuilder UseProblemDetailsExceptionHandling(this IApp
var officePath = settings.GetBackOfficePath(hostingEnvironment);
// Only use the API exception handler when we are requesting an API
// FIXME: magic string "management/api" is used several times across the codebase
return httpContext.Request.Path.Value?.StartsWith($"{officePath}/management/api/") ?? false;
return httpContext.Request.Path.Value?.StartsWith($"{officePath}{Constants.Web.ManagementApiPath}") ?? false;
},
innerBuilder =>
{
Expand Down Expand Up @@ -65,8 +64,7 @@ internal static IApplicationBuilder UseEndpoints(this IApplicationBuilder applic
endpoints.MapControllers();
// Serve contract
// FIXME: magic string "management/api" is used several times across the codebase
endpoints.MapGet($"{officePath}/management/api/openapi.json", async context =>
endpoints.MapGet($"{officePath}{Constants.Web.ManagementApiPath}openapi.json", async context =>
{
await context.Response.SendFileAsync(new EmbeddedFileProvider(typeof(ManagementApiComposer).Assembly).GetFileInfo("OpenApi.json"));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@


using Umbraco.Cms.Core;
using Umbraco.Cms.Web.Common.Routing;

namespace Umbraco.Cms.Api.Management.Routing;

public class VersionedApiBackOfficeRouteAttribute : BackOfficeRouteAttribute
{
public VersionedApiBackOfficeRouteAttribute(string template)
: base($"management/api/v{{version:apiVersion}}/{template.TrimStart('/')}")
: base($"{Constants.Web.ManagementApiPath}v{{version:apiVersion}}/{template.TrimStart('/')}")
{
}
}
5 changes: 5 additions & 0 deletions src/Umbraco.Core/Constants-Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public const string
public const string BackOfficeLoginArea = "UmbracoLogin"; // Used for area routes of non-api controllers for login
}

/// <summary>
/// The "base" path to the Management API
/// </summary>
public const string ManagementApiPath = "/management/api/";

public static class Routing
{
public const string ControllerToken = "controller";
Expand Down
2 changes: 1 addition & 1 deletion src/Umbraco.Core/Routing/UmbracoRequestPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public UmbracoRequestPaths(IOptions<GlobalSettings> globalSettings, IHostingEnvi
_previewMvcPath = "/" + mvcArea + "/Preview/";
_surfaceMvcPath = "/" + mvcArea + "/Surface/";
_apiMvcPath = "/" + mvcArea + "/Api/";
_managementApiPath = "/" + mvcArea + "/management/api/";
_managementApiPath = "/" + mvcArea + Constants.Web.ManagementApiPath;
_installPath = hostingEnvironment.ToAbsolute(Constants.SystemDirectories.Install);
_umbracoRequestPathsOptions = umbracoRequestPathsOptions;
}
Expand Down

0 comments on commit 1c79980

Please sign in to comment.