< Summary - Code Coverage

Information
Class: Plainquire.Page.Swashbuckle.Filters.EntityPageParameterReplacer
Assembly: Plainquire.Page.Swashbuckle
File(s): /home/runner/work/plainquire/plainquire/Plainquire.Page/Plainquire.Page.Swashbuckle/Filters/EntityPageParameterReplacer.cs
Tag: 64_13932151703
Line coverage
96%
Covered lines: 29
Uncovered lines: 1
Coverable lines: 30
Total lines: 77
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%
IsEntityPageParameter(...)50%22100%
GetConfiguration(...)66.66%6675%

File(s)

/home/runner/work/plainquire/plainquire/Plainquire.Page/Plainquire.Page.Swashbuckle/Filters/EntityPageParameterReplacer.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc.ApiExplorer;
 2using Microsoft.Extensions.DependencyInjection;
 3using Microsoft.Extensions.Options;
 4using Microsoft.OpenApi.Models;
 5using Plainquire.Page.Abstractions;
 6using Plainquire.Page.Swashbuckle.Models;
 7using Swashbuckle.AspNetCore.SwaggerGen;
 8using System;
 9using System.Diagnostics.CodeAnalysis;
 10using System.Linq;
 11
 12namespace Plainquire.Page.Swashbuckle.Filters;
 13
 14/// <summary>
 15/// Replaces action parameters of type <see cref="EntityPage"/> with page properties of type <c>TEntity</c>.
 16/// Implements <see cref="IOperationFilter" />
 17/// </summary>
 18/// <seealso cref="IOperationFilter" />
 19public class EntityPageParameterReplacer : IOperationFilter
 20{
 21    private readonly IServiceProvider _serviceProvider;
 22    private readonly PageConfiguration _defaultConfiguration;
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="EntityPageParameterReplacer"/> class.
 26    /// </summary>
 27    /// <param name="serviceProvider"></param>
 28    public EntityPageParameterReplacer(IServiceProvider serviceProvider)
 29    {
 2630        _serviceProvider = serviceProvider;
 2631        _defaultConfiguration = _serviceProvider.GetService<IOptions<PageConfiguration>>()?.Value ?? PageConfiguration.D
 2632    }
 33
 34    /// <summary>
 35    /// Replaces all parameters of type <see cref="EntityPage{TEntity}"/> with their applicable page properties.
 36    /// </summary>
 37    /// <param name="operation">The operation.</param>
 38    /// <param name="context">The context.</param>
 39    public void Apply(OpenApiOperation operation, OperationFilterContext context)
 40    {
 2341        var parametersToReplace = operation.Parameters
 2342            .Join(
 2343                context.ApiDescription.ParameterDescriptions,
 2344                parameter => parameter.Name,
 2345                description => description.Name,
 2346                (parameter, description) => (Parameter: parameter, Description: description),
 2347                StringComparer.Ordinal
 2348            )
 2349            .Where(openApi => IsEntityPageParameter(openApi.Description))
 2350            .Select(openApi =>
 2351            {
 2352                var entityPageType = openApi.Description.ParameterDescriptor.ParameterType;
 2353                var configuration = GetConfiguration(entityPageType);
 2354                return new PageParameterReplacement(
 2355                    OpenApiParameter: openApi.Parameter,
 2356                    OpenApiDescription: openApi.Description,
 2357                    PagedType: entityPageType.IsGenericType ? entityPageType.GenericTypeArguments[0] : null,
 2358                    Configuration: configuration);
 2359            })
 2360            .ToList();
 61
 2362        operation.ReplacePageParameters(parametersToReplace);
 2363    }
 64
 65    [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet
 66    private static bool IsEntityPageParameter(ApiParameterDescription description)
 6367        => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsAssignableTo(typeo
 68
 69    private PageConfiguration GetConfiguration(Type entityPageType)
 70    {
 4371        if (!entityPageType.IsEntityPage())
 072            throw new ArgumentException("Type is not an EntityPage", nameof(entityPageType));
 73
 4374        var entityTypeConfiguration = ((EntityPage?)_serviceProvider.GetService(entityPageType))?.Configuration;
 4375        return entityTypeConfiguration ?? _defaultConfiguration;
 76    }
 77}