< 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: 74_23635074410
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%
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    {
 2731        _serviceProvider = serviceProvider;
 2732        _defaultConfiguration = _serviceProvider.GetService<IOptions<PageConfiguration>>()?.Value ?? PageConfiguration.D
 2733    }
 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    {
 2542        operation.Parameters ??= new List<IOpenApiParameter>();
 2543        operation.AddOriginalIndexExtensionIfMissing(context);
 44
 2545        var parametersToReplace = context.ApiDescription.ParameterDescriptions
 2546            .Join(
 2547                operation.Parameters,
 2548                description => context.ApiDescription.ParameterDescriptions.IndexOf(description),
 2549                parameter => parameter.GetOriginalIndex(),
 2550                (description, parameter) => (Parameter: parameter, Description: description)
 2551            )
 2552            .Where(openApi => IsEntityPageParameter(openApi.Description))
 2553            .Select(openApi =>
 2554            {
 2555                var entityPageType = openApi.Description.ParameterDescriptor.ParameterType;
 2556                var configuration = GetConfiguration(entityPageType);
 2557                return new PageParameterReplacement(
 2558                    OpenApiParameter: openApi.Parameter,
 2559                    OpenApiDescription: openApi.Description,
 2560                    PagedType: entityPageType.IsGenericType ? entityPageType.GenericTypeArguments[0] : null,
 2561                    Configuration: configuration);
 2562            })
 2563            .ToList();
 64
 2565        operation.ReplacePageParameters(parametersToReplace);
 2566    }
 67
 68    [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet
 69    private static bool IsEntityPageParameter(ApiParameterDescription description)
 7470        => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsAssignableTo(typeo
 71
 72    private PageConfiguration GetConfiguration(Type entityPageType)
 73    {
 5474        if (!entityPageType.IsEntityPage())
 075            throw new ArgumentException("Type is not an EntityPage", nameof(entityPageType));
 76
 5477        var entityTypeConfiguration = ((EntityPage?)_serviceProvider.GetService(entityPageType))?.Configuration;
 5478        return entityTypeConfiguration ?? _defaultConfiguration;
 79    }
 80}