aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Swagger
diff options
context:
space:
mode:
Diffstat (limited to 'BackEnd/Timeline/Swagger')
-rw-r--r--BackEnd/Timeline/Swagger/ApiConvention.cs15
-rw-r--r--BackEnd/Timeline/Swagger/ByteDataRequestOperationProcessor.cs27
-rw-r--r--BackEnd/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs39
-rw-r--r--BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs55
4 files changed, 136 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Swagger/ApiConvention.cs b/BackEnd/Timeline/Swagger/ApiConvention.cs
new file mode 100644
index 00000000..dbf0b2fe
--- /dev/null
+++ b/BackEnd/Timeline/Swagger/ApiConvention.cs
@@ -0,0 +1,15 @@
+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/BackEnd/Timeline/Swagger/ByteDataRequestOperationProcessor.cs b/BackEnd/Timeline/Swagger/ByteDataRequestOperationProcessor.cs
new file mode 100644
index 00000000..887831ac
--- /dev/null
+++ b/BackEnd/Timeline/Swagger/ByteDataRequestOperationProcessor.cs
@@ -0,0 +1,27 @@
+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/BackEnd/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs b/BackEnd/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs
new file mode 100644
index 00000000..4967cc6a
--- /dev/null
+++ b/BackEnd/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs
@@ -0,0 +1,39 @@
+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/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs b/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs
new file mode 100644
index 00000000..dc5ddd96
--- /dev/null
+++ b/BackEnd/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs
@@ -0,0 +1,55 @@
+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();
+ }
+ }
+}