|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using CommunityToolkit.Datasync.Server.Filters; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.AspNetCore.Mvc.Controllers; |
| 8 | +using Microsoft.AspNetCore.OpenApi; |
| 9 | +using Microsoft.OpenApi.Models; |
| 10 | + |
| 11 | +namespace CommunityToolkit.Datasync.Server.OpenApi; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// The document transformer for the Datasync services. |
| 15 | +/// </summary> |
| 16 | +public class DatasyncOperationTransformer : IOpenApiOperationTransformer |
| 17 | +{ |
| 18 | + /// <inheritdoc /> |
| 19 | + public async Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) |
| 20 | + { |
| 21 | + // Determine if this operation is in scope. |
| 22 | + if (!IsDatasyncController(context)) |
| 23 | + { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Ensure the operation is set up for modification. |
| 28 | + operation.Parameters ??= []; |
| 29 | + |
| 30 | + string? actionName = context.Description.ActionDescriptor.RouteValues["action"]; |
| 31 | + if (actionName?.StartsWith("Create", StringComparison.InvariantCultureIgnoreCase) == true) |
| 32 | + { |
| 33 | + await TransformCreateAsync(operation, context, cancellationToken).ConfigureAwait(false); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + if (actionName?.StartsWith("Delete", StringComparison.InvariantCultureIgnoreCase) == true) |
| 38 | + { |
| 39 | + await TransformDeleteAsync(operation, context, cancellationToken).ConfigureAwait(false); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (actionName?.StartsWith("Query", StringComparison.InvariantCultureIgnoreCase) == true) |
| 44 | + { |
| 45 | + await TransformQueryAsync(operation, context, cancellationToken).ConfigureAwait(false); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (actionName?.StartsWith("Read", StringComparison.InvariantCultureIgnoreCase) == true) |
| 50 | + { |
| 51 | + await TransformReadAsync(operation, context, cancellationToken).ConfigureAwait(false); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Determines if a controller presented is a datasync controller. |
| 60 | + /// </summary> |
| 61 | + /// <param name="context">The transformer context.</param> |
| 62 | + /// <returns>true if the controller is a datasync controller; false otherwise.</returns> |
| 63 | + internal static bool IsDatasyncController(OpenApiOperationTransformerContext context) |
| 64 | + => context.Description.ActionDescriptor.FilterDescriptors.Any(fd => fd.Filter is DatasyncControllerAttribute); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Retrieves the entity type for the controller. |
| 68 | + /// </summary> |
| 69 | + /// <param name="context">The transformer context.</param> |
| 70 | + /// <returns>The type of the entity being served.</returns> |
| 71 | + internal static Type GetEntityType(OpenApiOperationTransformerContext context) |
| 72 | + { |
| 73 | + Type? baseType = (context.Description.ActionDescriptor as ControllerActionDescriptor)?.ControllerTypeInfo.AsType(); |
| 74 | + while (baseType is not null) |
| 75 | + { |
| 76 | + if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(TableController<>)) |
| 77 | + { |
| 78 | + Type? entityType = baseType.GetGenericArguments().FirstOrDefault(); |
| 79 | + if (entityType is not null && typeof(ITableData).IsAssignableFrom(entityType)) |
| 80 | + { |
| 81 | + return entityType; |
| 82 | + } |
| 83 | + |
| 84 | + throw new InvalidOperationException("Expecting the entity type to implement ITableData."); |
| 85 | + } |
| 86 | + |
| 87 | + baseType = baseType.BaseType; |
| 88 | + } |
| 89 | + |
| 90 | + throw new InvalidOperationException("Expecting the controller to be derived from TableController<T>."); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Transforms a create operation. |
| 95 | + /// </summary> |
| 96 | + /// <param name="operation">The operation to transform.</param> |
| 97 | + /// <param name="context">The operation transformer context.</param> |
| 98 | + /// <param name="cancellationToken">A cancellation token to observe.</param> |
| 99 | + /// <returns>A task that resolves when the operation is complete.</returns> |
| 100 | + internal Task TransformCreateAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) |
| 101 | + { |
| 102 | + Type entityType = GetEntityType(context); |
| 103 | + |
| 104 | + operation.Responses.AddEntityResponse(201, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 105 | + operation.Responses.AddStatusCode(StatusCodes.Status400BadRequest); |
| 106 | + operation.Responses.AddEntityResponse(409, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 107 | + operation.Responses.AddEntityResponse(412, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 108 | + |
| 109 | + return Task.CompletedTask; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Transforms a delete operation. |
| 114 | + /// </summary> |
| 115 | + /// <param name="operation">The operation to transform.</param> |
| 116 | + /// <param name="context">The operation transformer context.</param> |
| 117 | + /// <param name="cancellationToken">A cancellation token to observe.</param> |
| 118 | + /// <returns>A task that resolves when the operation is complete.</returns> |
| 119 | + internal Task TransformDeleteAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) |
| 120 | + { |
| 121 | + Type entityType = GetEntityType(context); |
| 122 | + |
| 123 | + operation.Parameters.AddIfMatchHeader(); |
| 124 | + operation.Parameters.AddIfUnmodifiedSinceHeader(); |
| 125 | + |
| 126 | + operation.Responses.AddStatusCode(StatusCodes.Status400BadRequest); |
| 127 | + operation.Responses.AddStatusCode(StatusCodes.Status404NotFound); |
| 128 | + operation.Responses.AddStatusCode(StatusCodes.Status410Gone); |
| 129 | + |
| 130 | + operation.Responses.AddEntityResponse(409, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 131 | + operation.Responses.AddEntityResponse(412, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 132 | + |
| 133 | + return Task.CompletedTask; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Transforms a query operation. |
| 138 | + /// </summary> |
| 139 | + /// <param name="operation">The operation to transform.</param> |
| 140 | + /// <param name="context">The operation transformer context.</param> |
| 141 | + /// <param name="cancellationToken">A cancellation token to observe.</param> |
| 142 | + /// <returns>A task that resolves when the operation is complete.</returns> |
| 143 | + internal Task TransformQueryAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) |
| 144 | + { |
| 145 | + Type entityType = GetEntityType(context); |
| 146 | + Type pagedEntityType = typeof(PagedResult<>).MakeGenericType(entityType); |
| 147 | + |
| 148 | + operation.Parameters.AddBooleanQueryParameter("$count", "Whether to include the total count of items matching the query in the result"); |
| 149 | + operation.Parameters.AddStringQueryParameter("$filter", "The filter to apply to the query"); |
| 150 | + operation.Parameters.AddStringQueryParameter("$orderby", "The comma-separated list of ordering instructions to apply to the query"); |
| 151 | + operation.Parameters.AddStringQueryParameter("$select", "The comma-separated list of fields to return in the results"); |
| 152 | + operation.Parameters.AddIntQueryParameter("$skip", "The number of items to skip", 0); |
| 153 | + operation.Parameters.AddIntQueryParameter("$top", "The number of items to return", 1); |
| 154 | + operation.Parameters.AddIncludeDeletedQuery(); |
| 155 | + |
| 156 | + operation.Responses.AddEntityResponse(200, context.GetSchemaForType(pagedEntityType), includeConditionalHeaders: false); |
| 157 | + operation.Responses.AddStatusCode(StatusCodes.Status400BadRequest); |
| 158 | + |
| 159 | + return Task.CompletedTask; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Transforms a read operation. |
| 164 | + /// </summary> |
| 165 | + /// <param name="operation">The operation to transform.</param> |
| 166 | + /// <param name="context">The operation transformer context.</param> |
| 167 | + /// <param name="cancellationToken">A cancellation token to observe.</param> |
| 168 | + /// <returns>A task that resolves when the operation is complete.</returns> |
| 169 | + internal Task TransformReadAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) |
| 170 | + { |
| 171 | + Type entityType = GetEntityType(context); |
| 172 | + |
| 173 | + operation.Parameters.AddIncludeDeletedQuery(); |
| 174 | + operation.Parameters.AddIfNoneMatchHeader(); |
| 175 | + operation.Parameters.AddIfModifiedSinceHeader(); |
| 176 | + |
| 177 | + operation.Responses.AddEntityResponse(200, context.GetSchemaForType(entityType), includeConditionalHeaders: true); |
| 178 | + operation.Responses.AddStatusCode(StatusCodes.Status304NotModified); |
| 179 | + operation.Responses.AddStatusCode(StatusCodes.Status404NotFound); |
| 180 | + operation.Responses.AddStatusCode(StatusCodes.Status410Gone); |
| 181 | + |
| 182 | + return Task.CompletedTask; |
| 183 | + } |
| 184 | +} |
0 commit comments