From ac769e656b122ff569c3f1534701b71e00fed586 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- Timeline/Swagger/ApiConvention.cs | 15 ------ .../Swagger/ByteDataRequestOperationProcessor.cs | 27 ----------- .../DefaultDescriptionOperationProcessor.cs | 39 --------------- .../DocumentDescriptionDocumentProcessor.cs | 55 ---------------------- 4 files changed, 136 deletions(-) delete mode 100644 Timeline/Swagger/ApiConvention.cs delete mode 100644 Timeline/Swagger/ByteDataRequestOperationProcessor.cs delete mode 100644 Timeline/Swagger/DefaultDescriptionOperationProcessor.cs delete mode 100644 Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs (limited to 'Timeline/Swagger') diff --git a/Timeline/Swagger/ApiConvention.cs b/Timeline/Swagger/ApiConvention.cs deleted file mode 100644 index dbf0b2fe..00000000 --- a/Timeline/Swagger/ApiConvention.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -[assembly: ApiConventionType(typeof(Timeline.Controllers.ApiConvention))] - -namespace Timeline.Controllers -{ - // There is some bug if nullable is enable. So disable it. -#nullable disable - /// - /// My api convention. - /// - public static class ApiConvention - { - } -} diff --git a/Timeline/Swagger/ByteDataRequestOperationProcessor.cs b/Timeline/Swagger/ByteDataRequestOperationProcessor.cs deleted file mode 100644 index 887831ac..00000000 --- a/Timeline/Swagger/ByteDataRequestOperationProcessor.cs +++ /dev/null @@ -1,27 +0,0 @@ -using NJsonSchema; -using NSwag; -using NSwag.Generation.Processors; -using NSwag.Generation.Processors.Contexts; -using System.Linq; -using Timeline.Models; - -namespace Timeline.Swagger -{ - /// - /// Coerce ByteData body type into the right one. - /// - public class ByteDataRequestOperationProcessor : IOperationProcessor - { - /// - public bool Process(OperationProcessorContext context) - { - var hasByteDataBody = context.MethodInfo.GetParameters().Where(p => p.ParameterType == typeof(ByteData)).Any(); - if (hasByteDataBody) - { - var bodyParameter = context.OperationDescription.Operation.Parameters.Where(p => p.Kind == OpenApiParameterKind.Body).Single(); - bodyParameter.Schema = JsonSchema.FromType(); - } - return true; - } - } -} diff --git a/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs b/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs deleted file mode 100644 index 4967cc6a..00000000 --- a/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs +++ /dev/null @@ -1,39 +0,0 @@ -using NSwag.Generation.Processors; -using NSwag.Generation.Processors.Contexts; -using System.Collections.Generic; - -namespace Timeline.Swagger -{ - /// - /// Swagger operation processor that adds default description to response. - /// - public class DefaultDescriptionOperationProcessor : IOperationProcessor - { - private readonly Dictionary defaultDescriptionMap = new Dictionary - { - ["200"] = "Succeeded to perform the operation.", - ["304"] = "Item does not change.", - ["400"] = "See code and message for error info.", - ["401"] = "You need to log in to perform this operation.", - ["403"] = "You have no permission to perform the operation.", - ["404"] = "Item does not exist. See code and message for error info." - }; - - /// - public bool Process(OperationProcessorContext context) - { - var responses = context.OperationDescription.Operation.Responses; - - foreach (var (httpStatusCode, res) in responses) - { - if (!string.IsNullOrEmpty(res.Description)) continue; - if (defaultDescriptionMap.ContainsKey(httpStatusCode)) - { - res.Description = defaultDescriptionMap[httpStatusCode]; - } - } - - return true; - } - } -} diff --git a/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs b/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs deleted file mode 100644 index dc5ddd96..00000000 --- a/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs +++ /dev/null @@ -1,55 +0,0 @@ -using NSwag.Generation.Processors; -using NSwag.Generation.Processors.Contexts; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Timeline.Models.Http; - -namespace Timeline.Swagger -{ - public class DocumentDescriptionDocumentProcessor : IDocumentProcessor - { - private static Dictionary GetAllErrorCodes() - { - var errorCodes = new Dictionary(); - - void RecursiveCheckErrorCode(Type type) - { - foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) - .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int))) - { - var name = (type.FullName + "." + field.Name).Remove(0, typeof(ErrorCodes).FullName!.Length + 1).Replace("+", ".", StringComparison.OrdinalIgnoreCase); - int value = (int)field.GetRawConstantValue()!; - errorCodes.Add(name, value); - } - - foreach (var nestedType in type.GetNestedTypes()) - { - RecursiveCheckErrorCode(nestedType); - } - } - - RecursiveCheckErrorCode(typeof(ErrorCodes)); - - return errorCodes; - } - - public void Process(DocumentProcessorContext context) - { - StringBuilder description = new StringBuilder(); - description.AppendLine("# Error Codes"); - description.AppendLine("name | value"); - description.AppendLine("---- | -----"); - foreach (var (name, value) in GetAllErrorCodes()) - { - description.AppendLine($"`{name}` | `{value}`"); - } - - context.Document.Info.Description = description.ToString(); - } - } -} -- cgit v1.2.3