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