| | 1 | | using Microsoft.AspNetCore.Mvc.ApiExplorer; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using Microsoft.OpenApi.Models; |
| | 5 | | using Plainquire.Sort.Abstractions; |
| | 6 | | using Plainquire.Sort.Swashbuckle.Models; |
| | 7 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 8 | | using System; |
| | 9 | | using System.Diagnostics.CodeAnalysis; |
| | 10 | | using System.Linq; |
| | 11 | |
|
| | 12 | | namespace Plainquire.Sort.Swashbuckle.Filters; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Replaces action parameters of type <see cref="EntitySort"/> with sortable properties of type <c>TEntity</c>. |
| | 16 | | /// Implements <see cref="IOperationFilter" /> |
| | 17 | | /// </summary> |
| | 18 | | /// <seealso cref="IOperationFilter" /> |
| | 19 | | [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated via DI.")] |
| | 20 | | public class EntitySortParameterReplacer : IOperationFilter |
| | 21 | | { |
| | 22 | | private readonly IServiceProvider _serviceProvider; |
| | 23 | | private readonly SortConfiguration _defaultConfiguration; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="EntitySortParameterReplacer"/> class. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="serviceProvider"></param> |
| | 29 | | public EntitySortParameterReplacer(IServiceProvider serviceProvider) |
| | 30 | | { |
| 32 | 31 | | _serviceProvider = serviceProvider; |
| 32 | 32 | | _defaultConfiguration = _serviceProvider.GetService<IOptions<SortConfiguration>>()?.Value ?? SortConfiguration.D |
| 32 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Replaces all parameters of type <see cref="EntitySort{TEntity}"/> with their applicable sort order 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 | | { |
| 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.Ordinal |
| 29 | 49 | | ) |
| 29 | 50 | | .Where(openApi => IsEntitySortParameter(openApi.Description)) |
| 29 | 51 | | .Select(openApi => |
| 29 | 52 | | { |
| 29 | 53 | | var entitySortType = openApi.Description.ParameterDescriptor.ParameterType; |
| 29 | 54 | | var configuration = GetConfiguration(entitySortType); |
| 29 | 55 | | return new SortParameterReplacement( |
| 29 | 56 | | OpenApiParameter: openApi.Parameter, |
| 29 | 57 | | OpenApiDescription: openApi.Description, |
| 29 | 58 | | SortedType: entitySortType.GenericTypeArguments[0], |
| 29 | 59 | | Configuration: configuration); |
| 29 | 60 | | }) |
| 29 | 61 | | .ToList(); |
| | 62 | |
|
| 29 | 63 | | operation.ReplaceSortParameters(parametersToReplace); |
| 29 | 64 | | } |
| | 65 | |
|
| | 66 | | [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet |
| | 67 | | private static bool IsEntitySortParameter(ApiParameterDescription description) |
| 106 | 68 | | => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsGenericEntitySort( |
| | 69 | |
|
| | 70 | | private SortConfiguration GetConfiguration(Type entitySortType) |
| | 71 | | { |
| 45 | 72 | | if (!entitySortType.IsGenericEntitySort()) |
| 0 | 73 | | throw new ArgumentException("Type is not an EntitySort<>", nameof(entitySortType)); |
| | 74 | |
|
| 45 | 75 | | var entityTypeConfiguration = ((EntitySort?)_serviceProvider.GetService(entitySortType))?.Configuration; |
| 45 | 76 | | return entityTypeConfiguration ?? _defaultConfiguration; |
| | 77 | | } |
| | 78 | | } |