diff options
author | crupest <crupest@outlook.com> | 2020-08-23 16:12:33 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-08-23 16:12:33 +0800 |
commit | ed5b550e8a19be250952371c60e1c1f3d2864a1e (patch) | |
tree | 7a4e61e0dc98a030a9350dc4f8339c6efbb65a6b | |
parent | c28848a35b0f31a59f9d02641571495822ad0db8 (diff) | |
download | timeline-ed5b550e8a19be250952371c60e1c1f3d2864a1e.tar.gz timeline-ed5b550e8a19be250952371c60e1c1f3d2864a1e.tar.bz2 timeline-ed5b550e8a19be250952371c60e1c1f3d2864a1e.zip |
Add error code to swagger description.
-rw-r--r-- | Timeline/Startup.cs | 5 | ||||
-rw-r--r-- | Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs | 55 |
2 files changed, 58 insertions, 2 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 86bdaf54..cbca01d0 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -104,6 +104,7 @@ namespace Timeline document.DocumentName = "Timeline";
document.Title = "Timeline REST API Reference";
document.Version = typeof(Startup).Assembly.GetName().Version?.ToString() ?? "unknown version";
+ document.DocumentProcessors.Add(new DocumentDescriptionDocumentProcessor());
document.DocumentProcessors.Add(
new SecurityDefinitionAppender("JWT",
new OpenApiSecurityScheme
@@ -111,7 +112,7 @@ namespace Timeline Type = OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
- Description = "Type into the textbox: Bearer {your JWT token}."
+ Description = "Create token via `/api/token/create` ."
}));
document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
document.OperationProcessors.Add(new DefaultDescriptionOperationProcessor());
@@ -153,7 +154,7 @@ namespace Timeline }
app.UseOpenApi();
- app.UseSwaggerUi3();
+ app.UseReDoc();
app.UseAuthentication();
app.UseAuthorization();
diff --git a/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs b/Timeline/Swagger/DocumentDescriptionDocumentProcessor.cs new file mode 100644 index 00000000..dc5ddd96 --- /dev/null +++ b/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();
+ }
+ }
+}
|