|
| 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 NJsonSchema; |
| 7 | +using NSwag; |
| 8 | +using NSwag.Generation.Processors; |
| 9 | +using NSwag.Generation.Processors.Contexts; |
| 10 | +using System.Net; |
| 11 | +using System.Reflection; |
| 12 | + |
| 13 | +namespace CommunityToolkit.Datasync.Server.NSwag; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Implements an <see cref="IOperationProcessor"/> for handling datasync table controllers. |
| 17 | +/// </summary> |
| 18 | +public class DatasyncOperationProcessor : IOperationProcessor |
| 19 | +{ |
| 20 | + /// <summary>Processes the specified method information.</summary> |
| 21 | + /// <param name="context">The processor context.</param> |
| 22 | + /// <returns>true if the operation should be added to the Swagger specification.</returns> |
| 23 | + public bool Process(OperationProcessorContext context) |
| 24 | + { |
| 25 | + if (IsTableController(context.ControllerType)) |
| 26 | + { |
| 27 | + ProcessDatasyncOperation(context); |
| 28 | + } |
| 29 | + |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Determines if the controller type provided is a datasync table controller. |
| 35 | + /// </summary> |
| 36 | + /// <param name="type">The type of the table controller.</param> |
| 37 | + /// <returns><c>true</c> if the type is a datasync table controller.</returns> |
| 38 | + internal static bool IsTableController(Type type) |
| 39 | + { |
| 40 | + if (!type.IsAbstract && type.BaseType != null && type.BaseType.IsGenericType == true) |
| 41 | + { |
| 42 | + if (type.GetCustomAttribute<DatasyncControllerAttribute>() != null) |
| 43 | + { |
| 44 | + return true; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Returns the entity type being handled by the controller type. |
| 53 | + /// </summary> |
| 54 | + /// <param name="controllerType">The <see cref="Type"/> of the controller.</param> |
| 55 | + /// <returns>The Type for the entity.</returns> |
| 56 | + /// <exception cref="ArgumentException">If the controller type is not a generic type.</exception> |
| 57 | + internal static Type GetTableEntityType(Type controllerType) |
| 58 | + => controllerType.BaseType?.GetGenericArguments().FirstOrDefault() |
| 59 | + ?? throw new ArgumentException("Unable to retrieve generic entity type"); |
| 60 | + |
| 61 | + private static void ProcessDatasyncOperation(OperationProcessorContext context) |
| 62 | + { |
| 63 | + OpenApiOperation operation = context.OperationDescription.Operation; |
| 64 | + string method = context.OperationDescription.Method; |
| 65 | + string path = context.OperationDescription.Path; |
| 66 | + Type entityType = GetTableEntityType(context.ControllerType); |
| 67 | + JsonSchema entitySchemaRef = GetEntityReference(context, entityType); |
| 68 | + |
| 69 | + if (method.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase)) |
| 70 | + { |
| 71 | + operation.AddConditionalRequestSupport(entitySchemaRef); |
| 72 | + operation.SetResponse(HttpStatusCode.NoContent); |
| 73 | + operation.SetResponse(HttpStatusCode.NotFound); |
| 74 | + operation.SetResponse(HttpStatusCode.Gone); |
| 75 | + } |
| 76 | + |
| 77 | + if (method.Equals("GET", StringComparison.InvariantCultureIgnoreCase) && path.EndsWith("/{id}")) |
| 78 | + { |
| 79 | + operation.AddConditionalRequestSupport(entitySchemaRef, true); |
| 80 | + operation.SetResponse(HttpStatusCode.OK, entitySchemaRef); |
| 81 | + operation.SetResponse(HttpStatusCode.NotFound); |
| 82 | + } |
| 83 | + |
| 84 | + if (method.Equals("GET", StringComparison.InvariantCultureIgnoreCase) && !path.EndsWith("/{id}")) |
| 85 | + { |
| 86 | + operation.AddODataQueryParameters(); |
| 87 | + operation.SetResponse(HttpStatusCode.OK, CreateListSchema(entitySchemaRef, entityType.Name), false); |
| 88 | + operation.SetResponse(HttpStatusCode.BadRequest); |
| 89 | + } |
| 90 | + |
| 91 | + if (method.Equals("POST", StringComparison.InvariantCultureIgnoreCase)) |
| 92 | + { |
| 93 | + operation.AddConditionalRequestSupport(entitySchemaRef, true); |
| 94 | + operation.SetResponse(HttpStatusCode.Created, entitySchemaRef); |
| 95 | + operation.SetResponse(HttpStatusCode.BadRequest); |
| 96 | + } |
| 97 | + |
| 98 | + if (method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase)) |
| 99 | + { |
| 100 | + operation.AddConditionalRequestSupport(entitySchemaRef); |
| 101 | + operation.SetResponse(HttpStatusCode.OK, entitySchemaRef); |
| 102 | + operation.SetResponse(HttpStatusCode.BadRequest); |
| 103 | + operation.SetResponse(HttpStatusCode.NotFound); |
| 104 | + operation.SetResponse(HttpStatusCode.Gone); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Either reads or generates the required entity type schema. |
| 110 | + /// </summary> |
| 111 | + /// <param name="context">The context for the operation processor.</param> |
| 112 | + /// <param name="entityType">The entity type needed.</param> |
| 113 | + /// <returns>A reference to the entity schema.</returns> |
| 114 | + private static JsonSchema GetEntityReference(OperationProcessorContext context, Type entityType) |
| 115 | + { |
| 116 | + string schemaName = context.SchemaGenerator.Settings.SchemaNameGenerator.Generate(entityType); |
| 117 | + if (!context.Document.Definitions.TryGetValue(schemaName, out JsonSchema? value)) |
| 118 | + { |
| 119 | + JsonSchema newSchema = context.SchemaGenerator.Generate(entityType); |
| 120 | + value = newSchema; |
| 121 | + context.Document.Definitions.Add(schemaName, value); |
| 122 | + } |
| 123 | + |
| 124 | + JsonSchema actualSchema = value; |
| 125 | + return new JsonSchema { Reference = actualSchema }; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Creates the paged item schema reference. |
| 130 | + /// </summary> |
| 131 | + /// <param name="entitySchema">The entity schema reference.</param> |
| 132 | + /// <param name="entityName">The name of the entity handled by the list operation.</param> |
| 133 | + /// <returns>The list schema reference</returns> |
| 134 | + private static JsonSchema CreateListSchema(JsonSchema entitySchema, string entityName) |
| 135 | + { |
| 136 | + JsonSchema listSchemaRef = new() |
| 137 | + { |
| 138 | + Description = $"A page of {entityName} entities", |
| 139 | + Type = JsonObjectType.Object |
| 140 | + }; |
| 141 | + listSchemaRef.Properties["items"] = new JsonSchemaProperty |
| 142 | + { |
| 143 | + Description = "The entities in this page of results", |
| 144 | + Type = JsonObjectType.Array, |
| 145 | + Item = entitySchema, |
| 146 | + IsReadOnly = true, |
| 147 | + IsNullableRaw = true |
| 148 | + }; |
| 149 | + listSchemaRef.Properties["count"] = new JsonSchemaProperty |
| 150 | + { |
| 151 | + Description = "The count of all entities in the result set", |
| 152 | + Type = JsonObjectType.Integer, |
| 153 | + IsReadOnly = true, |
| 154 | + IsNullableRaw = true |
| 155 | + }; |
| 156 | + listSchemaRef.Properties["nextLink"] = new JsonSchemaProperty |
| 157 | + { |
| 158 | + Description = "The URI to the next page of entities", |
| 159 | + Type = JsonObjectType.String, |
| 160 | + Format = "uri", |
| 161 | + IsReadOnly = true, |
| 162 | + IsNullableRaw = true |
| 163 | + }; |
| 164 | + return listSchemaRef; |
| 165 | + } |
| 166 | +} |
0 commit comments