< Summary - Syki

Information
Class: Syki.Back.Filters.HttpMethodSorterDocumentFilter
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Filters/HttpMethodSorterDocumentFilter.cs
Tag: 4_16869239191
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 50
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 13
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Apply(...)0%182130%

File(s)

/home/runner/work/syki/syki/Back/Filters/HttpMethodSorterDocumentFilter.cs

#LineLine coverage
 1using Microsoft.OpenApi.Models;
 2using Swashbuckle.AspNetCore.SwaggerGen;
 3
 4namespace Syki.Back.Filters;
 5
 6public class HttpMethodSorterDocumentFilter : IDocumentFilter
 7{
 8    public void Apply(OpenApiDocument document, DocumentFilterContext context)
 9    {
 10        // Order path groups (OpenApiPathItems) alphabetically
 011        var pathKvps = document.Paths
 012            .OrderBy(pathKvp =>
 013            {
 014                if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Post)) return 10;
 015                if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Get)) return 20;
 016                if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Put)) return 30;
 017                if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Delete)) return 40;
 018                return 50;
 019            })
 020            .ToList();
 21
 022        document.Paths.Clear();
 023        pathKvps.ForEach(kvp => document.Paths.Add(kvp.Key, kvp.Value));
 24
 25        // Order operations by method within each group
 026        document.Paths.ToList().ForEach(pathKvp =>
 027        {
 028            var operationKvps = pathKvp.Value.Operations
 029                .OrderBy(kvp =>
 030                {
 031                    var weight = kvp.Key switch
 032                    {
 033                        OperationType.Post => 10,
 034                        OperationType.Get => 20,
 035                        OperationType.Put => 30,
 036                        OperationType.Delete => 40,
 037                        _ => 50,
 038                    };
 039                    return weight;
 040                })
 041                .ToList();
 042
 043            pathKvp.Value.Operations.Clear();
 044            operationKvps.ForEach(operationKvp =>
 045            {
 046                pathKvp.Value.Operations.Add(operationKvp);
 047            });
 048        });
 049    }
 50}