From 9c087f584d48a4af646e29f7f4f98e2a94742dbd Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 20 Dec 2022 08:53:37 +0100 Subject: [PATCH] Fixed build warnings in ManagementApi --- ...troller.cs => ByIdDictionaryController.cs} | 0 .../Dictionary/ImportDictionaryController.cs | 6 +- .../Controllers/Security/Paths.cs | 4 +- ...e.cs => TrackedReferenceControllerBase.cs} | 0 .../ManagementApiComposer.cs | 91 +++++++++++-------- .../Services/UploadFileService.cs | 12 ++- .../Umbraco.Cms.ManagementApi.csproj | 3 - 7 files changed, 65 insertions(+), 51 deletions(-) rename src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/{ByKeyDictionaryController.cs => ByIdDictionaryController.cs} (100%) rename src/Umbraco.Cms.ManagementApi/Controllers/TrackedReference/{TrackedReferencesControllerBase.cs => TrackedReferenceControllerBase.cs} (100%) diff --git a/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ByKeyDictionaryController.cs b/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ByIdDictionaryController.cs similarity index 100% rename from src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ByKeyDictionaryController.cs rename to src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ByIdDictionaryController.cs diff --git a/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ImportDictionaryController.cs b/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ImportDictionaryController.cs index cbf9a0b9c7..b3c21446c9 100644 --- a/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ImportDictionaryController.cs +++ b/src/Umbraco.Cms.ManagementApi/Controllers/Dictionary/ImportDictionaryController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Core; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Services; using Umbraco.Cms.ManagementApi.Services; @@ -13,18 +14,15 @@ namespace Umbraco.Cms.ManagementApi.Controllers.Dictionary; public class ImportDictionaryController : DictionaryControllerBase { - private readonly IHostingEnvironment _hostingEnvironment; private readonly IDictionaryService _dictionaryService; private readonly IWebHostEnvironment _webHostEnvironment; private readonly ILoadDictionaryItemService _loadDictionaryItemService; public ImportDictionaryController( - IHostingEnvironment hostingEnvironment, IDictionaryService dictionaryService, IWebHostEnvironment webHostEnvironment, ILoadDictionaryItemService loadDictionaryItemService) { - _hostingEnvironment = hostingEnvironment; _dictionaryService = dictionaryService; _webHostEnvironment = webHostEnvironment; _loadDictionaryItemService = loadDictionaryItemService; @@ -41,7 +39,7 @@ public class ImportDictionaryController : DictionaryControllerBase return NotFound(); } - var filePath = Path.Combine(_hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.Data), file); + var filePath = Path.Combine(_webHostEnvironment.MapPathContentRoot(Constants.SystemDirectories.Data), file); if (_webHostEnvironment.ContentRootFileProvider.GetFileInfo(filePath) is null) { return await Task.FromResult(NotFound()); diff --git a/src/Umbraco.Cms.ManagementApi/Controllers/Security/Paths.cs b/src/Umbraco.Cms.ManagementApi/Controllers/Security/Paths.cs index 3ce1b7c3c6..77d69d90ab 100644 --- a/src/Umbraco.Cms.ManagementApi/Controllers/Security/Paths.cs +++ b/src/Umbraco.Cms.ManagementApi/Controllers/Security/Paths.cs @@ -4,9 +4,9 @@ public static class Paths { public const string BackOfficeApiEndpointTemplate = "security/back-office"; - public static string BackOfficeApiAuthorizationEndpoint = BackOfficeApiEndpointPath($"{BackOfficeApiEndpointTemplate}/authorize"); + public static readonly string BackOfficeApiAuthorizationEndpoint = BackOfficeApiEndpointPath($"{BackOfficeApiEndpointTemplate}/authorize"); - public static string BackOfficeApiTokenEndpoint = BackOfficeApiEndpointPath($"{BackOfficeApiEndpointTemplate}/token"); + public static readonly string BackOfficeApiTokenEndpoint = BackOfficeApiEndpointPath($"{BackOfficeApiEndpointTemplate}/token"); private static string BackOfficeApiEndpointPath(string relativePath) => $"/umbraco/management/api/v1.0/{relativePath}"; } diff --git a/src/Umbraco.Cms.ManagementApi/Controllers/TrackedReference/TrackedReferencesControllerBase.cs b/src/Umbraco.Cms.ManagementApi/Controllers/TrackedReference/TrackedReferenceControllerBase.cs similarity index 100% rename from src/Umbraco.Cms.ManagementApi/Controllers/TrackedReference/TrackedReferencesControllerBase.cs rename to src/Umbraco.Cms.ManagementApi/Controllers/TrackedReference/TrackedReferenceControllerBase.cs diff --git a/src/Umbraco.Cms.ManagementApi/ManagementApiComposer.cs b/src/Umbraco.Cms.ManagementApi/ManagementApiComposer.cs index f11290e76e..88d7602fdd 100644 --- a/src/Umbraco.Cms.ManagementApi/ManagementApiComposer.cs +++ b/src/Umbraco.Cms.ManagementApi/ManagementApiComposer.cs @@ -10,15 +10,16 @@ using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; +using Umbraco.Cms.Core; using Umbraco.Cms.Core.Composing; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.DependencyInjection; using Umbraco.Cms.ManagementApi.Configuration; +using Umbraco.Cms.ManagementApi.Controllers.Security; using Umbraco.Cms.ManagementApi.DependencyInjection; using Umbraco.Cms.ManagementApi.OpenApi; using Umbraco.Cms.Web.Common.ApplicationBuilder; using Umbraco.Extensions; -using Umbraco.New.Cms.Core; using Umbraco.New.Cms.Core.Models.Configuration; using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; @@ -77,18 +78,22 @@ public class ManagementApiComposer : IComposer if (string.IsNullOrWhiteSpace(relativePath)) { - throw new Exception($"There is no relative path for controller action {e.ActionDescriptor.RouteValues["controller"]}"); + throw new Exception( + $"There is no relative path for controller action {e.ActionDescriptor.RouteValues["controller"]}"); } // Remove the prefixed base path with version, e.g. /umbraco/management/api/v1/tracked-reference/{id} => tracked-reference/{id} - var unprefixedRelativePath = OperationIdRegexes.VersionPrefixRegex().Replace(relativePath, string.Empty); + var unprefixedRelativePath = + OperationIdRegexes.VersionPrefixRegex().Replace(relativePath, string.Empty); // Remove template placeholders, e.g. tracked-reference/{id} => tracked-reference/Id - var formattedOperationId = OperationIdRegexes.TemplatePlaceholdersRegex().Replace(unprefixedRelativePath, m => $"By{m.Groups[1].Value.ToFirstUpper()}"); + var formattedOperationId = OperationIdRegexes.TemplatePlaceholdersRegex() + .Replace(unprefixedRelativePath, m => $"By{m.Groups[1].Value.ToFirstUpper()}"); // Remove dashes (-) and slashes (/) and convert the following letter to uppercase with // the word "By" in front, e.g. tracked-reference/Id => TrackedReferenceById - formattedOperationId = OperationIdRegexes.ToCamelCaseRegex().Replace(formattedOperationId, m => m.Groups[1].Value.ToUpper()); + formattedOperationId = OperationIdRegexes.ToCamelCaseRegex() + .Replace(formattedOperationId, m => m.Groups[1].Value.ToUpper()); // Return the operation ID with the formatted http method verb in front, e.g. GetTrackedReferenceById return $"{httpMethod}{formattedOperationId.ToFirstUpper()}"; @@ -99,33 +104,41 @@ public class ManagementApiComposer : IComposer { Title = ApiTitle, Version = DefaultApiVersion.ToString(), - Description = "This shows all APIs available in this version of Umbraco - including all the legacy apis that are available for backward compatibility" + Description = + "This shows all APIs available in this version of Umbraco - including all the legacy apis that are available for backward compatibility" }); swaggerGenOptions.DocInclusionPredicate((_, api) => !string.IsNullOrWhiteSpace(api.GroupName)); - swaggerGenOptions.TagActionsBy(api => new [] { api.GroupName }); + swaggerGenOptions.TagActionsBy(api => new[] { api.GroupName }); // see https://github.com/domaindrivendev/Swashbuckle.AspNetCore#change-operation-sort-order-eg-for-ui-sorting string ActionSortKeySelector(ApiDescription apiDesc) - => $"{apiDesc.GroupName}_{apiDesc.ActionDescriptor.AttributeRouteInfo?.Template ?? apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.ActionDescriptor.RouteValues["action"]}_{apiDesc.HttpMethod}"; + { + return + $"{apiDesc.GroupName}_{apiDesc.ActionDescriptor.AttributeRouteInfo?.Template ?? apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.ActionDescriptor.RouteValues["action"]}_{apiDesc.HttpMethod}"; + } + swaggerGenOptions.OrderActionsBy(ActionSortKeySelector); - swaggerGenOptions.AddSecurityDefinition("OAuth", new OpenApiSecurityScheme - { - In = ParameterLocation.Header, - Name = "Umbraco", - Type = SecuritySchemeType.OAuth2, - Description = "Umbraco Authentication", - Flows = new OpenApiOAuthFlows + swaggerGenOptions.AddSecurityDefinition( + "OAuth", + new OpenApiSecurityScheme { - AuthorizationCode = new OpenApiOAuthFlow + In = ParameterLocation.Header, + Name = "Umbraco", + Type = SecuritySchemeType.OAuth2, + Description = "Umbraco Authentication", + Flows = new OpenApiOAuthFlows { - AuthorizationUrl = new Uri(Controllers.Security.Paths.BackOfficeApiAuthorizationEndpoint, UriKind.Relative), - TokenUrl = new Uri(Controllers.Security.Paths.BackOfficeApiTokenEndpoint, UriKind.Relative) + AuthorizationCode = new OpenApiOAuthFlow + { + AuthorizationUrl = + new Uri(Paths.BackOfficeApiAuthorizationEndpoint, UriKind.Relative), + TokenUrl = new Uri(Paths.BackOfficeApiTokenEndpoint, UriKind.Relative) + } } - } - }); + }); swaggerGenOptions.AddSecurityRequirement(new OpenApiSecurityRequirement { @@ -134,13 +147,9 @@ public class ManagementApiComposer : IComposer { new OpenApiSecurityScheme { - Reference = new OpenApiReference - { - Id = "OAuth", - Type = ReferenceType.SecurityScheme - } + Reference = new OpenApiReference { Id = "OAuth", Type = ReferenceType.SecurityScheme } }, - new List { } + new List() } }); @@ -175,8 +184,10 @@ public class ManagementApiComposer : IComposer applicationBuilder.UseWhen( httpContext => { - GlobalSettings? settings = httpContext.RequestServices.GetRequiredService>().Value; - IHostingEnvironment hostingEnvironment = httpContext.RequestServices.GetRequiredService(); + GlobalSettings? settings = httpContext.RequestServices + .GetRequiredService>().Value; + IHostingEnvironment hostingEnvironment = + httpContext.RequestServices.GetRequiredService(); var officePath = settings.GetBackOfficePath(hostingEnvironment); return httpContext.Request.Path.Value?.StartsWith($"{officePath}/management/api/") ?? false; @@ -197,7 +208,7 @@ public class ManagementApiComposer : IComposer Detail = exception.StackTrace, Status = StatusCodes.Status500InternalServerError, Instance = exception.GetType().Name, - Type = "Error", + Type = "Error" }; await context.Response.WriteAsJsonAsync(response); })); @@ -216,14 +227,19 @@ public class ManagementApiComposer : IComposer applicationBuilder.UseSwagger(swaggerOptions => { - swaggerOptions.RouteTemplate = $"{officePath.TrimStart(Core.Constants.CharArrays.ForwardSlash)}/swagger/{{documentName}}/swagger.json"; + swaggerOptions.RouteTemplate = + $"{officePath.TrimStart(Constants.CharArrays.ForwardSlash)}/swagger/{{documentName}}/swagger.json"; }); - applicationBuilder.UseSwaggerUI(swaggerUiOptions => + applicationBuilder.UseSwaggerUI( + swaggerUiOptions => { - swaggerUiOptions.SwaggerEndpoint($"{officePath}/swagger/v1/swagger.json", $"{ApiTitle} {DefaultApiVersion}"); - swaggerUiOptions.RoutePrefix = $"{officePath.TrimStart(Core.Constants.CharArrays.ForwardSlash)}/swagger"; + swaggerUiOptions.SwaggerEndpoint( + $"{officePath}/swagger/v1/swagger.json", + $"{ApiTitle} {DefaultApiVersion}"); + swaggerUiOptions.RoutePrefix = + $"{officePath.TrimStart(Constants.CharArrays.ForwardSlash)}/swagger"; - swaggerUiOptions.OAuthClientId(Constants.OauthClientIds.Swagger); + swaggerUiOptions.OAuthClientId(New.Cms.Core.Constants.OauthClientIds.Swagger); swaggerUiOptions.OAuthUsePkce(); }); } @@ -243,11 +259,12 @@ public class ManagementApiComposer : IComposer // Serve contract endpoints.MapGet($"{officePath}/management/api/openapi.json", async context => { - await context.Response.SendFileAsync(new EmbeddedFileProvider(GetType().Assembly).GetFileInfo("OpenApi.json")); + await context.Response.SendFileAsync( + new EmbeddedFileProvider(GetType().Assembly).GetFileInfo("OpenApi.json")); }); }); - } - )); + })); }); } } + diff --git a/src/Umbraco.Cms.ManagementApi/Services/UploadFileService.cs b/src/Umbraco.Cms.ManagementApi/Services/UploadFileService.cs index 20a43511ea..27de5dc545 100644 --- a/src/Umbraco.Cms.ManagementApi/Services/UploadFileService.cs +++ b/src/Umbraco.Cms.ManagementApi/Services/UploadFileService.cs @@ -1,22 +1,24 @@ using System.Xml; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.FileProviders; +using Microsoft.Extensions.Hosting; using Umbraco.Cms.Core; -using Umbraco.Cms.Core.Hosting; +using Umbraco.Cms.Core.Extensions; using Umbraco.Cms.Core.Services; using Umbraco.Cms.ManagementApi.Models; using Umbraco.Extensions; +using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; namespace Umbraco.Cms.ManagementApi.Services; public class UploadFileService : IUploadFileService { - private readonly IHostingEnvironment _hostingEnvironment; + private readonly IHostEnvironment _hostEnvironment; private readonly ILocalizedTextService _localizedTextService; - public UploadFileService(IHostingEnvironment hostingEnvironment, ILocalizedTextService localizedTextService) + public UploadFileService(IHostEnvironment hostEnvironment, ILocalizedTextService localizedTextService) { - _hostingEnvironment = hostingEnvironment; + _hostEnvironment = hostEnvironment; _localizedTextService = localizedTextService; } @@ -25,7 +27,7 @@ public class UploadFileService : IUploadFileService var formFileUploadResult = new FormFileUploadResult(); var fileName = file.FileName.Trim(Constants.CharArrays.DoubleQuote); var ext = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower(); - var root = _hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.TempFileUploads); + var root = _hostEnvironment.MapPathContentRoot(Constants.SystemDirectories.TempFileUploads); formFileUploadResult.TemporaryPath = Path.Combine(root, fileName); if (!Path.GetFullPath(formFileUploadResult.TemporaryPath).StartsWith(Path.GetFullPath(root))) diff --git a/src/Umbraco.Cms.ManagementApi/Umbraco.Cms.ManagementApi.csproj b/src/Umbraco.Cms.ManagementApi/Umbraco.Cms.ManagementApi.csproj index 3fe326cf44..8c2d96457e 100644 --- a/src/Umbraco.Cms.ManagementApi/Umbraco.Cms.ManagementApi.csproj +++ b/src/Umbraco.Cms.ManagementApi/Umbraco.Cms.ManagementApi.csproj @@ -13,9 +13,6 @@ - - -