diff options
Diffstat (limited to 'Timeline/Swagger')
-rw-r--r-- | Timeline/Swagger/ApiConvention.cs | 15 | ||||
-rw-r--r-- | Timeline/Swagger/ByteDataRequestOperationProcessor.cs | 27 | ||||
-rw-r--r-- | Timeline/Swagger/DefaultDescriptionOperationProcessor.cs | 39 | ||||
-rw-r--r-- | Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs | 55 |
4 files changed, 0 insertions, 136 deletions
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
- /// <summary>
- /// My api convention.
- /// </summary>
- 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
-{
- /// <summary>
- /// Coerce ByteData body type into the right one.
- /// </summary>
- public class ByteDataRequestOperationProcessor : IOperationProcessor
- {
- /// <inheritdoc/>
- 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<byte[]>();
- }
- 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
-{
- /// <summary>
- /// Swagger operation processor that adds default description to response.
- /// </summary>
- public class DefaultDescriptionOperationProcessor : IOperationProcessor
- {
- private readonly Dictionary<string, string> defaultDescriptionMap = new Dictionary<string, string>
- {
- ["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."
- };
-
- /// <inheritdoc/>
- 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<string, int> GetAllErrorCodes()
- {
- var errorCodes = new Dictionary<string, int>();
-
- 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();
- }
- }
-}
|