| | | 1 | | using Microsoft.AspNetCore.Mvc.ApiExplorer; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using Microsoft.OpenApi; |
| | | 5 | | using Plainquire.Filter.Abstractions; |
| | | 6 | | using Plainquire.Sort.Abstractions; |
| | | 7 | | using Plainquire.Sort.Swashbuckle.Models; |
| | | 8 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | 9 | | using System; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using System.Diagnostics.CodeAnalysis; |
| | | 12 | | using System.Linq; |
| | | 13 | | using System.Reflection; |
| | | 14 | | |
| | | 15 | | namespace Plainquire.Sort.Swashbuckle.Filters; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Replaces action parameters of type <see cref="IOperationFilter"/> with filterable properties of type <c>TEntity</c>. |
| | | 19 | | /// Implements <see cref="IOperationFilter" /> |
| | | 20 | | /// </summary> |
| | | 21 | | /// <seealso cref="EntitySort" /> |
| | | 22 | | [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Created by reflection")] |
| | | 23 | | public class EntitySortSetParameterReplacer : IOperationFilter |
| | | 24 | | { |
| | | 25 | | private readonly IServiceProvider _serviceProvider; |
| | | 26 | | private readonly SortConfiguration _defaultConfiguration; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="EntitySortSetParameterReplacer"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="serviceProvider"></param> |
| | | 32 | | public EntitySortSetParameterReplacer(IServiceProvider serviceProvider) |
| | | 33 | | { |
| | 32 | 34 | | _serviceProvider = serviceProvider; |
| | 32 | 35 | | _defaultConfiguration = _serviceProvider.GetService<IOptions<SortConfiguration>>()?.Value ?? SortConfiguration.D |
| | 32 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public void Apply(OpenApiOperation operation, OperationFilterContext context) |
| | | 40 | | { |
| | 29 | 41 | | operation.Parameters ??= new List<IOpenApiParameter>(); |
| | 29 | 42 | | var parametersToReplace = operation.Parameters |
| | 29 | 43 | | .Join( |
| | 29 | 44 | | context.ApiDescription.ParameterDescriptions, |
| | 29 | 45 | | parameter => parameter.Name, |
| | 29 | 46 | | description => description.Name, |
| | 29 | 47 | | (parameter, description) => (Parameter: parameter, Description: description), |
| | 29 | 48 | | StringComparer.OrdinalIgnoreCase |
| | 29 | 49 | | ) |
| | 29 | 50 | | .Where(openApi => IsEntitySortSetParameter(openApi.Description)) |
| | 29 | 51 | | .SelectMany(openApi => |
| | 29 | 52 | | openApi.Description.ParameterDescriptor |
| | 29 | 53 | | .ParameterType |
| | 29 | 54 | | .GetProperties() |
| | 29 | 55 | | .Select(x => x.PropertyType) |
| | 29 | 56 | | .Where(type => type.IsGenericEntitySort()) |
| | 29 | 57 | | .Select(entitySortType => new SortParameterReplacement( |
| | 29 | 58 | | OpenApiParameter: openApi.Parameter, |
| | 29 | 59 | | OpenApiDescription: openApi.Description, |
| | 29 | 60 | | SortedType: entitySortType.GenericTypeArguments[0], |
| | 29 | 61 | | Configuration: GetConfiguration(entitySortType)) |
| | 29 | 62 | | ) |
| | 29 | 63 | | ) |
| | 29 | 64 | | .ToList(); |
| | | 65 | | |
| | 29 | 66 | | operation.ReplaceSortParameters(parametersToReplace); |
| | 29 | 67 | | } |
| | | 68 | | |
| | | 69 | | [SuppressMessage("ReSharper", "ConditionalAccessQualifierIsNonNullableAccordingToAPIContract", Justification = "Para |
| | | 70 | | private static bool IsEntitySortSetParameter(ApiParameterDescription description) |
| | 90 | 71 | | => description.ParameterDescriptor?.ParameterType.GetCustomAttribute<EntitySortSetAttribute>() != null; |
| | | 72 | | |
| | | 73 | | private SortConfiguration GetConfiguration(Type entitySortType) |
| | | 74 | | { |
| | 30 | 75 | | if (!entitySortType.IsGenericEntitySort()) |
| | 0 | 76 | | throw new ArgumentException("Type is not an EntitySort<>", nameof(entitySortType)); |
| | | 77 | | |
| | 30 | 78 | | var entityTypeConfiguration = ((EntitySort?)_serviceProvider.GetService(entitySortType))?.Configuration; |
| | 30 | 79 | | return entityTypeConfiguration ?? _defaultConfiguration; |
| | | 80 | | } |
| | | 81 | | } |