< 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: 70_19770578580
Line coverage
96%
Covered lines: 30
Uncovered lines: 1
Coverable lines: 31
Total lines: 80
Line coverage: 96.7%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
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(...)50%22100%
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;
 5using Plainquire.Sort.Abstractions;
 6using Plainquire.Sort.Swashbuckle.Models;
 7using Swashbuckle.AspNetCore.SwaggerGen;
 8using System;
 9using System.Collections.Generic;
 10using System.Diagnostics.CodeAnalysis;
 11using System.Linq;
 12
 13namespace Plainquire.Sort.Swashbuckle.Filters;
 14
 15/// <summary>
 16/// Replaces action parameters of type <see cref="EntitySort"/> with sortable properties of type <c>TEntity</c>.
 17/// Implements <see cref="IOperationFilter" />
 18/// </summary>
 19/// <seealso cref="IOperationFilter" />
 20[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated via DI.")]
 21public class EntitySortParameterReplacer : IOperationFilter
 22{
 23    private readonly IServiceProvider _serviceProvider;
 24    private readonly SortConfiguration _defaultConfiguration;
 25
 26    /// <summary>
 27    /// Initializes a new instance of the <see cref="EntitySortParameterReplacer"/> class.
 28    /// </summary>
 29    /// <param name="serviceProvider"></param>
 30    public EntitySortParameterReplacer(IServiceProvider serviceProvider)
 31    {
 3232        _serviceProvider = serviceProvider;
 3233        _defaultConfiguration = _serviceProvider.GetService<IOptions<SortConfiguration>>()?.Value ?? SortConfiguration.D
 3234    }
 35
 36    /// <summary>
 37    /// Replaces all parameters of type <see cref="EntitySort{TEntity}"/> with their applicable sort order properties.
 38    /// </summary>
 39    /// <param name="operation">The operation.</param>
 40    /// <param name="context">The context.</param>
 41    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 42    {
 2943        operation.Parameters ??= new List<IOpenApiParameter>();
 2944        var parametersToReplace = operation.Parameters
 2945            .Join(
 2946                context.ApiDescription.ParameterDescriptions,
 2947                parameter => parameter.Name,
 2948                description => description.Name,
 2949                (parameter, description) => (Parameter: parameter, Description: description),
 2950                StringComparer.OrdinalIgnoreCase
 2951            )
 2952            .Where(openApi => IsEntitySortParameter(openApi.Description))
 2953            .Select(openApi =>
 2954            {
 2955                var entitySortType = openApi.Description.ParameterDescriptor.ParameterType;
 2956                var configuration = GetConfiguration(entitySortType);
 2957                return new SortParameterReplacement(
 2958                    OpenApiParameter: openApi.Parameter,
 2959                    OpenApiDescription: openApi.Description,
 2960                    SortedType: entitySortType.GenericTypeArguments[0],
 2961                    Configuration: configuration);
 2962            })
 2963            .ToList();
 64
 2965        operation.ReplaceSortParameters(parametersToReplace);
 2966    }
 67
 68    [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet
 69    private static bool IsEntitySortParameter(ApiParameterDescription description)
 10670        => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsGenericEntitySort(
 71
 72    private SortConfiguration GetConfiguration(Type entitySortType)
 73    {
 4574        if (!entitySortType.IsGenericEntitySort())
 075            throw new ArgumentException("Type is not an EntitySort<>", nameof(entitySortType));
 76
 4577        var entityTypeConfiguration = ((EntitySort?)_serviceProvider.GetService(entitySortType))?.Configuration;
 4578        return entityTypeConfiguration ?? _defaultConfiguration;
 79    }
 80}