< 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: 70_19770578580
Line coverage
96%
Covered lines: 30
Uncovered lines: 1
Coverable lines: 31
Total lines: 79
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%
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;
 5using Plainquire.Page.Abstractions;
 6using Plainquire.Page.Swashbuckle.Models;
 7using Swashbuckle.AspNetCore.SwaggerGen;
 8using System;
 9using System.Collections.Generic;
 10using System.Diagnostics.CodeAnalysis;
 11using System.Linq;
 12
 13namespace Plainquire.Page.Swashbuckle.Filters;
 14
 15/// <summary>
 16/// Replaces action parameters of type <see cref="EntityPage"/> with page properties of type <c>TEntity</c>.
 17/// Implements <see cref="IOperationFilter" />
 18/// </summary>
 19/// <seealso cref="IOperationFilter" />
 20public class EntityPageParameterReplacer : IOperationFilter
 21{
 22    private readonly IServiceProvider _serviceProvider;
 23    private readonly PageConfiguration _defaultConfiguration;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="EntityPageParameterReplacer"/> class.
 27    /// </summary>
 28    /// <param name="serviceProvider"></param>
 29    public EntityPageParameterReplacer(IServiceProvider serviceProvider)
 30    {
 2631        _serviceProvider = serviceProvider;
 2632        _defaultConfiguration = _serviceProvider.GetService<IOptions<PageConfiguration>>()?.Value ?? PageConfiguration.D
 2633    }
 34
 35    /// <summary>
 36    /// Replaces all parameters of type <see cref="EntityPage{TEntity}"/> with their applicable page 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    {
 2342        operation.Parameters ??= new List<IOpenApiParameter>();
 2343        var parametersToReplace = operation.Parameters
 2344            .Join(
 2345                context.ApiDescription.ParameterDescriptions,
 2346                parameter => parameter.Name,
 2347                description => description.Name,
 2348                (parameter, description) => (Parameter: parameter, Description: description),
 2349                StringComparer.Ordinal
 2350            )
 2351            .Where(openApi => IsEntityPageParameter(openApi.Description))
 2352            .Select(openApi =>
 2353            {
 2354                var entityPageType = openApi.Description.ParameterDescriptor.ParameterType;
 2355                var configuration = GetConfiguration(entityPageType);
 2356                return new PageParameterReplacement(
 2357                    OpenApiParameter: openApi.Parameter,
 2358                    OpenApiDescription: openApi.Description,
 2359                    PagedType: entityPageType.IsGenericType ? entityPageType.GenericTypeArguments[0] : null,
 2360                    Configuration: configuration);
 2361            })
 2362            .ToList();
 63
 2364        operation.ReplacePageParameters(parametersToReplace);
 2365    }
 66
 67    [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet
 68    private static bool IsEntityPageParameter(ApiParameterDescription description)
 6369        => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsAssignableTo(typeo
 70
 71    private PageConfiguration GetConfiguration(Type entityPageType)
 72    {
 4373        if (!entityPageType.IsEntityPage())
 074            throw new ArgumentException("Type is not an EntityPage", nameof(entityPageType));
 75
 4376        var entityTypeConfiguration = ((EntityPage?)_serviceProvider.GetService(entityPageType))?.Configuration;
 4377        return entityTypeConfiguration ?? _defaultConfiguration;
 78    }
 79}