Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs
Bjarke Berg 38947e0870 Added functionality to split det swagger doc into multiple definitions (#14126)
* Added functionality to split det swagger doc into multiple definitions. Most (maybe too much) is still shared between the apis.

* Fixed issue with duplicate operation ids when we have multiple versions of an endpoint

* use strong types instead of var

* Updated OpenApi.json

* Formatting

* formatting

* Delete old files that is not implemented in api.common

* Updated openAPi after merge

---------

Co-authored-by: Zeegaan <nge@umbraco.dk>
2023-04-20 12:38:31 +02:00

78 lines
3.4 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Extensions;
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
namespace Umbraco.Cms.Api.Management.DependencyInjection;
internal static class ApplicationBuilderExtensions
{
internal static IApplicationBuilder UseProblemDetailsExceptionHandling(this IApplicationBuilder applicationBuilder)
=> applicationBuilder.UseWhen(
httpContext =>
{
GlobalSettings settings = httpContext.RequestServices
.GetRequiredService<IOptions<GlobalSettings>>().Value;
IHostingEnvironment hostingEnvironment =
httpContext.RequestServices.GetRequiredService<IHostingEnvironment>();
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;
},
innerBuilder =>
{
innerBuilder.UseExceptionHandler(exceptionBuilder => exceptionBuilder.Run(async context =>
{
Exception? exception = context.Features.Get<IExceptionHandlerPathFeature>()?.Error;
if (exception is null)
{
return;
}
var response = new ProblemDetails
{
Title = exception.Message,
Detail = exception.StackTrace,
Status = StatusCodes.Status500InternalServerError,
Instance = exception.GetType().Name,
Type = "Error"
};
await context.Response.WriteAsJsonAsync(response);
}));
});
internal static IApplicationBuilder UseEndpoints(this IApplicationBuilder applicationBuilder)
{
IServiceProvider provider = applicationBuilder.ApplicationServices;
applicationBuilder.UseEndpoints(endpoints =>
{
GlobalSettings settings = provider.GetRequiredService<IOptions<GlobalSettings>>().Value;
IHostingEnvironment hostingEnvironment = provider.GetRequiredService<IHostingEnvironment>();
var officePath = settings.GetBackOfficePath(hostingEnvironment);
// Maps attribute routed controllers.
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 =>
{
await context.Response.SendFileAsync(new EmbeddedFileProvider(typeof(ManagementApiComposer).Assembly).GetFileInfo("OpenApi.json"));
});
});
return applicationBuilder;
}
}