| | | 1 | | using LoxSmoke.DocXml; |
| | | 2 | | using Microsoft.AspNetCore.Mvc.ApiExplorer; |
| | | 3 | | using Microsoft.OpenApi; |
| | | 4 | | using Plainquire.Filter.Abstractions; |
| | | 5 | | using Plainquire.Filter.Swashbuckle.Models; |
| | | 6 | | using Swashbuckle.AspNetCore.SwaggerGen; |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Reflection; |
| | | 12 | | using System.Text.Json.Nodes; |
| | | 13 | | |
| | | 14 | | namespace Plainquire.Filter.Swashbuckle; |
| | | 15 | | |
| | | 16 | | internal static class OpenApiParameterExtensions |
| | | 17 | | { |
| | | 18 | | private const string PARAMETER_INDEX_EXTENSION = "x-original-parameter-index"; |
| | | 19 | | internal const string ENTITY_EXTENSION_PREFIX = "x-entity-filter-"; |
| | | 20 | | |
| | | 21 | | [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract", Justification = "Paramet |
| | | 22 | | public static bool IsEntityFilterParameter(this ApiParameterDescription description) |
| | 113 | 23 | | => description.ParameterDescriptor != null && description.ParameterDescriptor.ParameterType.IsGenericEntityFilte |
| | | 24 | | |
| | | 25 | | [SuppressMessage("ReSharper", "ConditionalAccessQualifierIsNonNullableAccordingToAPIContract", Justification = "Para |
| | | 26 | | public static bool IsEntityFilterSetParameter(this ApiParameterDescription description) |
| | 85 | 27 | | => description.ParameterDescriptor?.ParameterType.GetCustomAttribute<EntityFilterSetAttribute>() != null; |
| | | 28 | | |
| | | 29 | | public static void ReplaceFilterParameters(this IList<IOpenApiParameter> parameters, List<FilterParameterReplaceInfo |
| | | 30 | | { |
| | 42 | 31 | | foreach (var replacement in parameterReplacements) |
| | | 32 | | { |
| | 9 | 33 | | if (!replacement.Parameters.Any()) |
| | | 34 | | continue; |
| | | 35 | | |
| | 9 | 36 | | var parameterIndex = parameters.IndexOf(replacement.Parameters[0]); |
| | | 37 | | |
| | 108 | 38 | | foreach (var parameter in replacement.Parameters) |
| | 45 | 39 | | parameters.Remove(parameter); |
| | | 40 | | |
| | 9 | 41 | | var propertyParameters = replacement |
| | 9 | 42 | | .EntityFilters |
| | 9 | 43 | | .SelectMany(entityFilterType => entityFilterType |
| | 9 | 44 | | .GenericTypeArguments[0] |
| | 9 | 45 | | .ExpandToPropertyParameters(docXmlReaders) |
| | 9 | 46 | | ) |
| | 9 | 47 | | .ToList(); |
| | | 48 | | |
| | 64 | 49 | | foreach (var parameter in propertyParameters) |
| | 23 | 50 | | parameters.Insert(parameterIndex++, parameter); |
| | | 51 | | } |
| | 12 | 52 | | } |
| | | 53 | | |
| | | 54 | | public static void AddOriginalIndexExtensionIfMissing(this OpenApiOperation operation, OperationFilterContext contex |
| | | 55 | | { |
| | 12 | 56 | | if (operation.Parameters == null) |
| | 0 | 57 | | return; |
| | | 58 | | |
| | 12 | 59 | | var indexExtensionsAlreadyAdded = operation.Parameters.Any(p => p.Extensions?.ContainsKey(PARAMETER_INDEX_EXTENS |
| | 12 | 60 | | if (indexExtensionsAlreadyAdded) |
| | 4 | 61 | | return; |
| | | 62 | | |
| | 264 | 63 | | for (var index = 0; index < operation.Parameters.Count; index++) |
| | | 64 | | { |
| | 124 | 65 | | var parameter = operation.Parameters[index]; |
| | 124 | 66 | | if (parameter is not IOpenApiExtensible openApiParameter) |
| | 0 | 67 | | throw new InvalidOperationException("The OpenApiParameter must implement IOpenApiExtensible to be replac |
| | | 68 | | |
| | 124 | 69 | | openApiParameter.Extensions ??= new Dictionary<string, IOpenApiExtension>(StringComparer.OrdinalIgnoreCase); |
| | 124 | 70 | | openApiParameter.Extensions.Add(PARAMETER_INDEX_EXTENSION, new JsonNodeExtension(JsonValue.Create(index))); |
| | | 71 | | } |
| | 8 | 72 | | } |
| | | 73 | | |
| | | 74 | | public static int GetOriginalIndex(this IOpenApiParameter parameter) |
| | | 75 | | { |
| | 214 | 76 | | if (parameter.Extensions == null) |
| | 0 | 77 | | return -1; |
| | | 78 | | |
| | 214 | 79 | | if (!parameter.Extensions.TryGetValue(PARAMETER_INDEX_EXTENSION, out var extension)) |
| | 8 | 80 | | return -1; |
| | | 81 | | |
| | 206 | 82 | | if (extension is not JsonNodeExtension nodeExtension) |
| | 0 | 83 | | throw new InvalidOperationException($"Extension '{PARAMETER_INDEX_EXTENSION}' must be of type {nameof(JsonNo |
| | | 84 | | |
| | 206 | 85 | | if (nodeExtension.Node is not JsonValue jsonValue) |
| | 0 | 86 | | throw new InvalidOperationException($"Value of extension '{PARAMETER_INDEX_EXTENSION}' must be of type {name |
| | | 87 | | |
| | 206 | 88 | | return jsonValue.GetValue<int>(); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private static List<OpenApiParameter> ExpandToPropertyParameters(this Type filteredType, IReadOnlyCollection<DocXmlR |
| | | 92 | | { |
| | 9 | 93 | | var filterableProperties = filteredType.GetFilterableProperties(); |
| | 9 | 94 | | var entityFilterAttribute = filteredType.GetCustomAttribute<EntityFilterAttribute>(); |
| | | 95 | | |
| | 9 | 96 | | return filterableProperties |
| | 9 | 97 | | .Select(property => new OpenApiParameter |
| | 9 | 98 | | { |
| | 9 | 99 | | Name = property.GetFilterParameterName(entityFilterAttribute?.Prefix), |
| | 9 | 100 | | Description = docXmlReaders.GetXmlDocumentationSummary(property), |
| | 9 | 101 | | Schema = new OpenApiSchema { Type = JsonSchemaType.String }, |
| | 9 | 102 | | In = ParameterLocation.Query, |
| | 9 | 103 | | Extensions = new Dictionary<string, IOpenApiExtension>(StringComparer.OrdinalIgnoreCase) |
| | 9 | 104 | | { |
| | 9 | 105 | | [ENTITY_EXTENSION_PREFIX + "property-type"] = new JsonNodeExtension(JsonValue.Create(property.Proper |
| | 9 | 106 | | } |
| | 9 | 107 | | }) |
| | 9 | 108 | | .ToList(); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private static string? GetXmlDocumentationSummary(this IEnumerable<DocXmlReader> docXmlReaders, MemberInfo member) |
| | 23 | 112 | | => docXmlReaders.Select(x => x.GetMemberComment(member)).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)); |
| | | 113 | | } |