From dcb0a10e1adaec24961f2484e7f9a9a1f1f5da15 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 21 Aug 2020 21:25:33 +0800 Subject: ... --- Timeline/Swagger/ApiConvention.cs | 15 +++++++++ .../DefaultDescriptionOperationProcessor.cs | 39 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Timeline/Swagger/ApiConvention.cs create mode 100644 Timeline/Swagger/DefaultDescriptionOperationProcessor.cs (limited to 'Timeline/Swagger') diff --git a/Timeline/Swagger/ApiConvention.cs b/Timeline/Swagger/ApiConvention.cs new file mode 100644 index 00000000..dbf0b2fe --- /dev/null +++ b/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 + /// + /// My api convention. + /// + public static class ApiConvention + { + } +} diff --git a/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs b/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs new file mode 100644 index 00000000..4967cc6a --- /dev/null +++ b/Timeline/Swagger/DefaultDescriptionOperationProcessor.cs @@ -0,0 +1,39 @@ +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; + } + } +} -- cgit v1.2.3 From be532c56158362701a4d5704e995c743ff8b9804 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 21 Aug 2020 23:24:47 +0800 Subject: ... --- Timeline/Startup.cs | 1 + .../Swagger/ByteDataRequestOperationProcessor.cs | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 Timeline/Swagger/ByteDataRequestOperationProcessor.cs (limited to 'Timeline/Swagger') diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 408057e5..86bdaf54 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -115,6 +115,7 @@ namespace Timeline })); document.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT")); document.OperationProcessors.Add(new DefaultDescriptionOperationProcessor()); + document.OperationProcessors.Add(new ByteDataRequestOperationProcessor()); }); if (!disableFrontEnd) diff --git a/Timeline/Swagger/ByteDataRequestOperationProcessor.cs b/Timeline/Swagger/ByteDataRequestOperationProcessor.cs new file mode 100644 index 00000000..887831ac --- /dev/null +++ b/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 +{ + /// + /// 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; + } + } +} -- cgit v1.2.3