| | | 1 | | using LoxSmoke.DocXml; |
| | | 2 | | using Microsoft.OpenApi; |
| | | 3 | | using Plainquire.Filter.Swashbuckle.Models; |
| | | 4 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text.Json.Nodes; |
| | | 10 | | |
| | | 11 | | namespace Plainquire.Filter.Swashbuckle.Filters; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Replaces action parameters of type <see cref="EntityFilter"/> with filterable properties of type <c>TEntity</c>. |
| | | 15 | | /// Implements <see cref="IOperationFilter" /> |
| | | 16 | | /// </summary> |
| | | 17 | | /// <seealso cref="IOperationFilter" /> |
| | | 18 | | [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Instantiated via reflection.")] |
| | | 19 | | public class EntityFilterParameterReplacer : IOperationFilter |
| | | 20 | | { |
| | | 21 | | private readonly List<DocXmlReader> _docXmlReaders; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="EntityFilterParameterReplacer"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="xmlDocumentationFilePaths">Paths to XML documentation files. Used to provide parameter descriptions |
| | | 27 | | public EntityFilterParameterReplacer(IEnumerable<string>? xmlDocumentationFilePaths) |
| | 6 | 28 | | => _docXmlReaders = xmlDocumentationFilePaths?.Select(x => new DocXmlReader(x)).ToList() ?? []; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Replaces all parameters of type <see cref="EntityFilter{TEntity}"/> with their applicable filter properties. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="operation">The operation.</param> |
| | | 34 | | /// <param name="context">The context.</param> |
| | | 35 | | public void Apply(OpenApiOperation operation, OperationFilterContext context) |
| | | 36 | | { |
| | 3 | 37 | | var parameterReplacements = GetEntityFilterReplacements(operation, context); |
| | 3 | 38 | | operation.Parameters ??= new List<IOpenApiParameter>(); |
| | 3 | 39 | | operation.Parameters.ReplaceFilterParameters(parameterReplacements, _docXmlReaders); |
| | | 40 | | |
| | 3 | 41 | | var hasParametersFromEntityFilter = parameterReplacements.Any(); |
| | 3 | 42 | | operation.Extensions ??= new Dictionary<string, IOpenApiExtension>(StringComparer.OrdinalIgnoreCase); |
| | 3 | 43 | | operation.Extensions[OpenApiParameterExtensions.ENTITY_EXTENSION_PREFIX + "has-filter-parameters"] = new JsonNod |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | private static List<FilterParameterReplaceInfo> GetEntityFilterReplacements(OpenApiOperation operation, OperationFil |
| | | 47 | | { |
| | 3 | 48 | | operation.Parameters ??= new List<IOpenApiParameter>(); |
| | 3 | 49 | | var parameterReplacements = operation.Parameters |
| | 3 | 50 | | .Join( |
| | 3 | 51 | | context.ApiDescription.ParameterDescriptions, |
| | 3 | 52 | | parameter => parameter.Name, |
| | 3 | 53 | | description => description.Name, |
| | 3 | 54 | | (parameter, description) => (Parameter: parameter, Description: description), |
| | 3 | 55 | | StringComparer.Ordinal |
| | 3 | 56 | | ) |
| | 3 | 57 | | .Where(openApi => openApi.Description.IsEntityFilterParameter()) |
| | 3 | 58 | | .GroupBy(openApi => openApi.Description.ParameterDescriptor.ParameterType) |
| | 3 | 59 | | .Select(parameterGroup => |
| | 3 | 60 | | { |
| | 3 | 61 | | var parametersToRemove = parameterGroup.Select(x => x.Parameter).ToList(); |
| | 3 | 62 | | var filteredTypesToAdd = new[] { parameterGroup.Key }.ToList(); |
| | 3 | 63 | | return new FilterParameterReplaceInfo(parametersToRemove, filteredTypesToAdd); |
| | 3 | 64 | | }) |
| | 3 | 65 | | .ToList(); |
| | | 66 | | |
| | 3 | 67 | | return parameterReplacements; |
| | | 68 | | } |
| | | 69 | | } |