< Summary

Information
Class: Elsa.FastEndpointConfigurators.ElsaFastEndpointsConfigurator
Assembly: Elsa.Api.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/FastEndpointConfigurators/ElsaFastEndpointsConfigurator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 50
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Configure(...)100%210%
DeserializeRequestAsync(...)0%620%
SerializeResponseAsync(...)0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Api.Common/FastEndpointConfigurators/ElsaFastEndpointsConfigurator.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using CShells.FastEndpoints.Contracts;
 5using Elsa.Workflows;
 6using FastEndpoints;
 7using JetBrains.Annotations;
 8using Microsoft.AspNetCore.Http;
 9using Microsoft.Extensions.DependencyInjection;
 10
 11namespace Elsa.FastEndpointConfigurators;
 12
 13/// <summary>
 14/// Configures FastEndpoints with Elsa-specific serialization options.
 15/// Uses the same serialization settings as <see cref="Elsa.Extensions.WebApplicationExtensions.UseWorkflowsApi"/>.
 16/// </summary>
 17[UsedImplicitly]
 18public class ElsaFastEndpointsConfigurator : IFastEndpointsConfigurator
 19{
 20    /// <inheritdoc />
 21    public void Configure(Config config)
 22    {
 023        config.Serializer.RequestDeserializer = DeserializeRequestAsync;
 024        config.Serializer.ResponseSerializer = SerializeResponseAsync;
 25
 026        config.Binding.ValueParserFor<DateTimeOffset>(s =>
 027            new(DateTimeOffset.TryParse(s.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out va
 028    }
 29
 30    private static ValueTask<object?> DeserializeRequestAsync(HttpRequest httpRequest, Type modelType, JsonSerializerCon
 31    {
 032        var serializer = httpRequest.HttpContext.RequestServices.GetRequiredService<IApiSerializer>();
 033        var options = serializer.GetOptions();
 34
 035        return serializerContext == null
 036            ? JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, options, cancellationToken)
 037            : JsonSerializer.DeserializeAsync(httpRequest.Body, modelType, serializerContext, cancellationToken);
 38    }
 39
 40    private static Task SerializeResponseAsync(HttpResponse httpResponse, object? dto, string contentType, JsonSerialize
 41    {
 042        var serializer = httpResponse.HttpContext.RequestServices.GetRequiredService<IApiSerializer>();
 043        var options = serializer.GetOptions();
 44
 045        httpResponse.ContentType = contentType;
 046        return serializerContext == null
 047            ? JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), options, cancellat
 048            : JsonSerializer.SerializeAsync(httpResponse.Body, dto, dto?.GetType() ?? typeof(object), serializerContext,
 49    }
 50}