< Summary - Code Coverage

Information
Class: Plainquire.Sort.Swashbuckle.Filters.EntitySortParameterReplacer
Assembly: Plainquire.Sort.Swashbuckle
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Sort/Plainquire.Sort.Swashbuckle/Filters/EntitySortParameterReplacer.cs
Tag: 64_13932151703
Line coverage
96%
Covered lines: 29
Uncovered lines: 1
Coverable lines: 30
Total lines: 78
Line coverage: 96.6%
Branch coverage
78%
Covered branches: 11
Total branches: 14
Branch coverage: 78.5%
Method coverage
100%
Covered methods: 4
Total methods: 4
Method coverage: 100%

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)83.33%66100%
Apply(...)100%11100%
IsEntitySortParameter(...)50%22100%
GetConfiguration(...)66.66%6675%

File(s)

/home/runner/work/plainquire/plainquire/Plainquire.Sort/Plainquire.Sort.Swashbuckle/Filters/EntitySortParameterReplacer.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc.ApiExplorer;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Options;
 4using Microsoft.OpenApi.Models;
 5using Plainquire.Sort.Abstractions;
 6using Plainquire.Sort.Swashbuckle.Models;
 7using Swashbuckle.AspNetCore.SwaggerGen;
 8using System;
 9using System.Diagnostics.CodeAnalysis;
 10using System.Linq;
 11
 12namespace Plainquire.Sort.Swashbuckle.Filters;
 13
 14/// <summary>
 15/// Replaces action parameters of type <see cref="EntitySort"/> with sortable properties of type <c>TEntity</c>.
 16/// Implements <see cref="IOperationFilter" />
 17/// </summary>
 18/// <seealso cref="IOperationFilter" />
 19[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated via DI.")]
 20public class EntitySortParameterReplacer : IOperationFilter
 21{
 22    private readonly IServiceProvider _serviceProvider;
 23    private readonly SortConfiguration _defaultConfiguration;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="EntitySortParameterReplacer"/> class.
 27    /// </summary>
 28    /// <param name="serviceProvider"></param>
 29    public EntitySortParameterReplacer(IServiceProvider serviceProvider)
 30    {
 3231        _serviceProvider = serviceProvider;
 3232        _defaultConfiguration = _serviceProvider.GetService<IOptions<SortConfiguration>>()?.Value ?? SortConfiguration.D
 3233    }
 34
 35    /// <summary>
 36    /// Replaces all parameters of type <see cref="EntitySort{TEntity}"/> with their applicable sort order properties.
 37    /// </summary>
 38    /// <param name="operation">The operation.</param>
 39    /// <param name="context">The context.</param>
 40    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 41    {
 2942        var parametersToReplace = operation.Parameters
 2943            .Join(
 2944                context.ApiDescription.ParameterDescriptions,
 2945                parameter => parameter.Name,
 2946                description => description.Name,
 2947                (parameter, description) => (Parameter: parameter, Description: description),
 2948                StringComparer.Ordinal
 2949            )
 2950            .Where(openApi => IsEntitySortParameter(openApi.Description))
 2951            .Select(openApi =>
 2952            {
 2953                var entitySortType = openApi.Description.ParameterDescriptor.ParameterType;
 2954                var configuration = GetConfiguration(entitySortType);
 2955                return new SortParameterReplacement(
 2956                    OpenApiParameter: openApi.Parameter,
 2957                    OpenApiDescription: openApi.Description,
 2958                    SortedType: entitySortType.GenericTypeArguments[0],
 2959                    Configuration: configuration);
 2960            })
 2961            .ToList();
 62
 2963        operation.ReplaceSortParameters(parametersToReplace);
 2964    }
 65
 66    [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet
 67    private static bool IsEntitySortParameter(ApiParameterDescription description)
 10668        => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsGenericEntitySort(
 69
 70    private SortConfiguration GetConfiguration(Type entitySortType)
 71    {
 4572        if (!entitySortType.IsGenericEntitySort())
 073            throw new ArgumentException("Type is not an EntitySort<>", nameof(entitySortType));
 74
 4575        var entityTypeConfiguration = ((EntitySort?)_serviceProvider.GetService(entitySortType))?.Configuration;
 4576        return entityTypeConfiguration ?? _defaultConfiguration;
 77    }
 78}