< Summary

Information
Class: Elsa.Extensions.WebApplicationExtensions
Assembly: Elsa.Api.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Extensions/WebApplicationExtensions.cs
Line coverage
76%
Covered lines: 20
Uncovered lines: 6
Coverable lines: 26
Total lines: 75
Line coverage: 76.9%
Branch coverage
33%
Covered branches: 4
Total branches: 12
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseWorkflowsApi(...)100%11100%
MapWorkflowsApi(...)100%210%
DeserializeRequestAsync(...)50%22100%
SerializeRequestAsync(...)30%1010100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/Extensions/WebApplicationExtensions.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Elsa.Workflows;
 5using FastEndpoints;
 6using JetBrains.Annotations;
 7using Microsoft.AspNetCore.Builder;
 8using Microsoft.AspNetCore.Http;
 9using Microsoft.AspNetCore.Routing;
 10using Microsoft.Extensions.DependencyInjection;
 11
 12// ReSharper disable once CheckNamespace
 13namespace Elsa.Extensions;
 14
 15/// <summary>
 16/// Provides extension methods to add the FastEndpoints middleware configured for use with Elsa API endpoints.
 17/// </summary>
 18[PublicAPI]
 19public static class WebApplicationExtensions
 20{
 21    /// <summary>
 22    /// Register the FastEndpoints middleware configured for use with with Elsa API endpoints.
 23    /// </summary>
 24    /// <param name="app"></param>
 25    /// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param>
 26    /// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example>
 27    public static IApplicationBuilder UseWorkflowsApi(this IApplicationBuilder app, string routePrefix = "elsa/api")
 28    {
 129        return app.UseFastEndpoints(config =>
 130        {
 131            config.Endpoints.RoutePrefix = routePrefix;
 132            config.Serializer.RequestDeserializer = DeserializeRequestAsync;
 133            config.Serializer.ResponseSerializer = SerializeRequestAsync;
 134
 135            config.Binding.ValueParserFor<DateTimeOffset>(s =>
 136                new(DateTimeOffset.TryParse(s.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, ou
 237        });
 38    }
 39
 40    /// <summary>
 41    /// Register the FastEndpoints middleware configured for use with with Elsa API endpoints.
 42    /// </summary>
 43    /// <param name="routes">The <see cref="IEndpointRouteBuilder"/> to register the endpoints with.</param>
 44    /// <param name="routePrefix">The route prefix to apply to Elsa API endpoints.</param>
 45    /// <example>E.g. "elsa/api" will expose endpoints like this: "/elsa/api/workflow-definitions"</example>
 46    public static IEndpointRouteBuilder MapWorkflowsApi(this IEndpointRouteBuilder routes, string routePrefix = "elsa/ap
 047        routes.MapFastEndpoints(config =>
 048        {
 049            config.Endpoints.RoutePrefix = routePrefix;
 050            config.Serializer.RequestDeserializer = DeserializeRequestAsync;
 051            config.Serializer.ResponseSerializer = SerializeRequestAsync;
 052        });
 53
 54    private static ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerCon
 55    {
 256        var serializer = httpRequest.HttpContext.RequestServices.GetRequiredService<IApiSerializer>();
 257        var options = serializer.GetOptions();
 58
 259        return serializerContext == null
 260            ? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken)
 261            : JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken);
 62    }
 63
 64    private static Task SerializeRequestAsync(HttpResponse httpResponse, object? dto, string contentType, JsonSerializer
 65    {
 366        var serializer = httpResponse.HttpContext.RequestServices.GetRequiredService<IApiSerializer>();
 367        var options = serializer.GetOptions();
 68
 369        httpResponse.ContentType = contentType;
 370        return serializerContext == null
 371            ? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), options, cancellat
 372            : JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), serializerContext,
 73    }
 74
 75}