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