< Summary - Code Coverage

Information
Class: Plainquire.Sort.Swashbuckle.Filters.EntitySortSetParameterReplacer
Assembly: Plainquire.Sort.Swashbuckle
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Sort/Plainquire.Sort.Swashbuckle/Filters/EntitySortSetParameterReplacer.cs
Tag: 66_15485642072
Line coverage
96%
Covered lines: 32
Uncovered lines: 1
Coverable lines: 33
Total lines: 79
Line coverage: 96.9%
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%
IsEntitySortSetParameter(...)50%22100%
GetConfiguration(...)66.66%6675%

File(s)

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

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc.ApiExplorer;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Options;
 4using Microsoft.OpenApi.Models;
 5using Plainquire.Filter.Abstractions;
 6using Plainquire.Sort.Abstractions;
 7using Plainquire.Sort.Swashbuckle.Models;
 8using Swashbuckle.AspNetCore.SwaggerGen;
 9using System;
 10using System.Diagnostics.CodeAnalysis;
 11using System.Linq;
 12using System.Reflection;
 13
 14namespace Plainquire.Sort.Swashbuckle.Filters;
 15
 16/// <summary>
 17/// Replaces action parameters of type <see cref="IOperationFilter"/> with filterable properties of type <c>TEntity</c>.
 18/// Implements <see cref="IOperationFilter" />
 19/// </summary>
 20/// <seealso cref="EntitySort" />
 21[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Created by reflection")]
 22public class EntitySortSetParameterReplacer : IOperationFilter
 23{
 24    private readonly IServiceProvider _serviceProvider;
 25    private readonly SortConfiguration _defaultConfiguration;
 26
 27    /// <summary>
 28    /// Initializes a new instance of the <see cref="EntitySortSetParameterReplacer"/> class.
 29    /// </summary>
 30    /// <param name="serviceProvider"></param>
 31    public EntitySortSetParameterReplacer(IServiceProvider serviceProvider)
 32    {
 3233        _serviceProvider = serviceProvider;
 3234        _defaultConfiguration = _serviceProvider.GetService<IOptions<SortConfiguration>>()?.Value ?? SortConfiguration.D
 3235    }
 36
 37    /// <inheritdoc />
 38    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 39    {
 2940        var parametersToReplace = operation.Parameters
 2941            .Join(
 2942                context.ApiDescription.ParameterDescriptions,
 2943                parameter => parameter.Name,
 2944                description => description.Name,
 2945                (parameter, description) => (Parameter: parameter, Description: description),
 2946                StringComparer.Ordinal
 2947            )
 2948            .Where(openApi => IsEntitySortSetParameter(openApi.Description))
 2949            .SelectMany(openApi =>
 2950                openApi.Description.ParameterDescriptor
 2951                    .ParameterType
 2952                    .GetProperties()
 2953                    .Select(x => x.PropertyType)
 2954                    .Where(type => type.IsGenericEntitySort())
 2955                    .Select(entitySortType => new SortParameterReplacement(
 2956                        OpenApiParameter: openApi.Parameter,
 2957                        OpenApiDescription: openApi.Description,
 2958                        SortedType: entitySortType.GenericTypeArguments[0],
 2959                        Configuration: GetConfiguration(entitySortType))
 2960                    )
 2961            )
 2962            .ToList();
 63
 2964        operation.ReplaceSortParameters(parametersToReplace);
 2965    }
 66
 67    [SuppressMessage("ReSharper", "ConditionalAccessQualifierIsNonNullableAccordingToAPIContract", Justification = "Para
 68    private static bool IsEntitySortSetParameter(ApiParameterDescription description)
 9069        => description.ParameterDescriptor?.ParameterType.GetCustomAttribute<EntitySortSetAttribute>() != null;
 70
 71    private SortConfiguration GetConfiguration(Type entitySortType)
 72    {
 3073        if (!entitySortType.IsGenericEntitySort())
 074            throw new ArgumentException("Type is not an EntitySort<>", nameof(entitySortType));
 75
 3076        var entityTypeConfiguration = ((EntitySort?)_serviceProvider.GetService(entitySortType))?.Configuration;
 3077        return entityTypeConfiguration ?? _defaultConfiguration;
 78    }
 79}