| | 1 | | using Microsoft.AspNetCore.Mvc.ApiExplorer; |
| | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | 3 | | using Microsoft.Extensions.Options; |
| | 4 | | using Microsoft.OpenApi.Models; |
| | 5 | | using Plainquire.Page.Abstractions; |
| | 6 | | using Plainquire.Page.Swashbuckle.Models; |
| | 7 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 8 | | using System; |
| | 9 | | using System.Diagnostics.CodeAnalysis; |
| | 10 | | using System.Linq; |
| | 11 | |
|
| | 12 | | namespace Plainquire.Page.Swashbuckle.Filters; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// Replaces action parameters of type <see cref="EntityPage"/> with page properties of type <c>TEntity</c>. |
| | 16 | | /// Implements <see cref="IOperationFilter" /> |
| | 17 | | /// </summary> |
| | 18 | | /// <seealso cref="IOperationFilter" /> |
| | 19 | | public class EntityPageParameterReplacer : IOperationFilter |
| | 20 | | { |
| | 21 | | private readonly IServiceProvider _serviceProvider; |
| | 22 | | private readonly PageConfiguration _defaultConfiguration; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="EntityPageParameterReplacer"/> class. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="serviceProvider"></param> |
| | 28 | | public EntityPageParameterReplacer(IServiceProvider serviceProvider) |
| | 29 | | { |
| 26 | 30 | | _serviceProvider = serviceProvider; |
| 26 | 31 | | _defaultConfiguration = _serviceProvider.GetService<IOptions<PageConfiguration>>()?.Value ?? PageConfiguration.D |
| 26 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Replaces all parameters of type <see cref="EntityPage{TEntity}"/> with their applicable page properties. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="operation">The operation.</param> |
| | 38 | | /// <param name="context">The context.</param> |
| | 39 | | public void Apply(OpenApiOperation operation, OperationFilterContext context) |
| | 40 | | { |
| 23 | 41 | | var parametersToReplace = operation.Parameters |
| 23 | 42 | | .Join( |
| 23 | 43 | | context.ApiDescription.ParameterDescriptions, |
| 23 | 44 | | parameter => parameter.Name, |
| 23 | 45 | | description => description.Name, |
| 23 | 46 | | (parameter, description) => (Parameter: parameter, Description: description), |
| 23 | 47 | | StringComparer.Ordinal |
| 23 | 48 | | ) |
| 23 | 49 | | .Where(openApi => IsEntityPageParameter(openApi.Description)) |
| 23 | 50 | | .Select(openApi => |
| 23 | 51 | | { |
| 23 | 52 | | var entityPageType = openApi.Description.ParameterDescriptor.ParameterType; |
| 23 | 53 | | var configuration = GetConfiguration(entityPageType); |
| 23 | 54 | | return new PageParameterReplacement( |
| 23 | 55 | | OpenApiParameter: openApi.Parameter, |
| 23 | 56 | | OpenApiDescription: openApi.Description, |
| 23 | 57 | | PagedType: entityPageType.IsGenericType ? entityPageType.GenericTypeArguments[0] : null, |
| 23 | 58 | | Configuration: configuration); |
| 23 | 59 | | }) |
| 23 | 60 | | .ToList(); |
| | 61 | |
|
| 23 | 62 | | operation.ReplacePageParameters(parametersToReplace); |
| 23 | 63 | | } |
| | 64 | |
|
| | 65 | | [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet |
| | 66 | | private static bool IsEntityPageParameter(ApiParameterDescription description) |
| 63 | 67 | | => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsAssignableTo(typeo |
| | 68 | |
|
| | 69 | | private PageConfiguration GetConfiguration(Type entityPageType) |
| | 70 | | { |
| 43 | 71 | | if (!entityPageType.IsEntityPage()) |
| 0 | 72 | | throw new ArgumentException("Type is not an EntityPage", nameof(entityPageType)); |
| | 73 | |
|
| 43 | 74 | | var entityTypeConfiguration = ((EntityPage?)_serviceProvider.GetService(entityPageType))?.Configuration; |
| 43 | 75 | | return entityTypeConfiguration ?? _defaultConfiguration; |
| | 76 | | } |
| | 77 | | } |