| | 1 | | using LoxSmoke.DocXml; |
| | 2 | | using Microsoft.OpenApi.Any; |
| | 3 | | using Microsoft.OpenApi.Models; |
| | 4 | | using Plainquire.Filter.Swashbuckle.Models; |
| | 5 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics.CodeAnalysis; |
| | 9 | | using System.Linq; |
| | 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.ReplaceFilterParameters(parameterReplacements, _docXmlReaders); |
| | 39 | |
|
| 3 | 40 | | var hasParametersFromEntityFilter = parameterReplacements.Any(); |
| 3 | 41 | | operation.Extensions[OpenApiParameterExtensions.ENTITY_EXTENSION_PREFIX + "has-filter-parameters"] = new OpenApi |
| 3 | 42 | | } |
| | 43 | |
|
| | 44 | | private static List<FilterParameterReplaceInfo> GetEntityFilterReplacements(OpenApiOperation operation, OperationFil |
| | 45 | | { |
| 3 | 46 | | var parameterReplacements = operation.Parameters |
| 3 | 47 | | .Join( |
| 3 | 48 | | context.ApiDescription.ParameterDescriptions, |
| 3 | 49 | | parameter => parameter.Name, |
| 3 | 50 | | description => description.Name, |
| 3 | 51 | | (parameter, description) => (Parameter: parameter, Description: description), |
| 3 | 52 | | StringComparer.Ordinal |
| 3 | 53 | | ) |
| 3 | 54 | | .Where(openApi => openApi.Description.IsEntityFilterParameter()) |
| 3 | 55 | | .GroupBy(openApi => openApi.Description.ParameterDescriptor.ParameterType) |
| 3 | 56 | | .Select(parameterGroup => |
| 3 | 57 | | { |
| 3 | 58 | | var parametersToRemove = parameterGroup.Select(x => x.Parameter).ToList(); |
| 3 | 59 | | var filteredTypesToAdd = new[] { parameterGroup.Key }.ToList(); |
| 3 | 60 | | return new FilterParameterReplaceInfo(parametersToRemove, filteredTypesToAdd); |
| 3 | 61 | | }) |
| 3 | 62 | | .ToList(); |
| | 63 | |
|
| 3 | 64 | | return parameterReplacements; |
| | 65 | | } |
| | 66 | | } |