From 34228caf2c846a42e6b3e5f6df08f83ed20cf8d8 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 20 Dec 2020 23:07:49 +0200 Subject: [PATCH] Replaced Swagger and MediatR implementations with inhouse nuget packages. --- .../Chatbot.Api.Application.csproj | 1 + Chatbot.Api.Application/Commands/CloseChat.cs | 1 + Chatbot.Api.Application/Commands/Command.cs | 18 --- Chatbot.Api.Application/Commands/Metadata.cs | 25 ---- .../Commands/SaveChatMessage.cs | 1 + .../Commands/TestLogger.cs | 1 + Chatbot.Api.Application/Queries/GetBots.cs | 1 + Chatbot.Api.Application/Queries/GetChat.cs | 1 + Chatbot.Api.Application/Queries/GetSession.cs | 1 + Chatbot.Api.Application/Queries/Query.cs | 8 -- Chatbot.Api/Chatbot.Api.csproj | 1 + Chatbot.Api/Startup.cs | 6 +- Chatbot.Api/Swagger/DtoSchemaFilter.cs | 36 ------ .../Swagger/PathParamsOperationFilter.cs | 34 ------ Chatbot.Api/Swagger/SwaggerExtensions.cs | 114 ------------------ dependencies.props | 2 + 16 files changed, 13 insertions(+), 238 deletions(-) delete mode 100644 Chatbot.Api.Application/Commands/Command.cs delete mode 100644 Chatbot.Api.Application/Commands/Metadata.cs delete mode 100644 Chatbot.Api.Application/Queries/Query.cs delete mode 100644 Chatbot.Api/Swagger/DtoSchemaFilter.cs delete mode 100644 Chatbot.Api/Swagger/PathParamsOperationFilter.cs delete mode 100644 Chatbot.Api/Swagger/SwaggerExtensions.cs diff --git a/Chatbot.Api.Application/Chatbot.Api.Application.csproj b/Chatbot.Api.Application/Chatbot.Api.Application.csproj index f09fd55..95f47b3 100644 --- a/Chatbot.Api.Application/Chatbot.Api.Application.csproj +++ b/Chatbot.Api.Application/Chatbot.Api.Application.csproj @@ -11,6 +11,7 @@ + diff --git a/Chatbot.Api.Application/Commands/CloseChat.cs b/Chatbot.Api.Application/Commands/CloseChat.cs index a112554..39cfda1 100644 --- a/Chatbot.Api.Application/Commands/CloseChat.cs +++ b/Chatbot.Api.Application/Commands/CloseChat.cs @@ -1,4 +1,5 @@ using Chatbot.Api.Application.Events; +using NDB.Application.DataContracts; using System; namespace Chatbot.Api.Application.Commands diff --git a/Chatbot.Api.Application/Commands/Command.cs b/Chatbot.Api.Application/Commands/Command.cs deleted file mode 100644 index f747213..0000000 --- a/Chatbot.Api.Application/Commands/Command.cs +++ /dev/null @@ -1,18 +0,0 @@ -using MediatR; - -namespace Chatbot.Api.Application.Commands -{ - public abstract class Command : ICommand, IRequest - { - public Metadata Metadata { get; } - - protected Command(Metadata metadata) - { - Metadata = metadata; - } - } - - public interface ICommand - { - } -} diff --git a/Chatbot.Api.Application/Commands/Metadata.cs b/Chatbot.Api.Application/Commands/Metadata.cs deleted file mode 100644 index 57d8cf7..0000000 --- a/Chatbot.Api.Application/Commands/Metadata.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Chatbot.Api.Application.Commands -{ - public class Metadata : Dictionary - { - public const string CorrelationIdKey = "CorrelationId"; - - public Guid CorrelationId - { - get - { - return Guid.Parse(this[CorrelationIdKey]); - } - set - { - if (ContainsKey(CorrelationIdKey)) - this[CorrelationIdKey] = value.ToString(); - else - Add(CorrelationIdKey, value.ToString()); - } - } - } -} diff --git a/Chatbot.Api.Application/Commands/SaveChatMessage.cs b/Chatbot.Api.Application/Commands/SaveChatMessage.cs index 42abd1a..f0a625d 100644 --- a/Chatbot.Api.Application/Commands/SaveChatMessage.cs +++ b/Chatbot.Api.Application/Commands/SaveChatMessage.cs @@ -1,4 +1,5 @@ using Chatbot.Api.Application.Events; +using NDB.Application.DataContracts; using System; namespace Chatbot.Api.Application.Commands diff --git a/Chatbot.Api.Application/Commands/TestLogger.cs b/Chatbot.Api.Application/Commands/TestLogger.cs index 01ee9b2..30d54b7 100644 --- a/Chatbot.Api.Application/Commands/TestLogger.cs +++ b/Chatbot.Api.Application/Commands/TestLogger.cs @@ -1,4 +1,5 @@ using Chatbot.Api.Application.Events; +using NDB.Application.DataContracts; using System; namespace Chatbot.Api.Application.Commands diff --git a/Chatbot.Api.Application/Queries/GetBots.cs b/Chatbot.Api.Application/Queries/GetBots.cs index c52e0cc..491e33b 100644 --- a/Chatbot.Api.Application/Queries/GetBots.cs +++ b/Chatbot.Api.Application/Queries/GetBots.cs @@ -1,6 +1,7 @@ using AutoMapper; using Chatbot.Api.Domain.Repositories; using MediatR; +using NDB.Application.DataContracts; using System; using System.Threading; using System.Threading.Tasks; diff --git a/Chatbot.Api.Application/Queries/GetChat.cs b/Chatbot.Api.Application/Queries/GetChat.cs index 4b56bf4..0f88abe 100644 --- a/Chatbot.Api.Application/Queries/GetChat.cs +++ b/Chatbot.Api.Application/Queries/GetChat.cs @@ -1,6 +1,7 @@ using AutoMapper; using Chatbot.Api.Domain.Repositories; using MediatR; +using NDB.Application.DataContracts; using System; using System.Threading; using System.Threading.Tasks; diff --git a/Chatbot.Api.Application/Queries/GetSession.cs b/Chatbot.Api.Application/Queries/GetSession.cs index 976aada..d20039e 100644 --- a/Chatbot.Api.Application/Queries/GetSession.cs +++ b/Chatbot.Api.Application/Queries/GetSession.cs @@ -1,6 +1,7 @@ using AutoMapper; using Chatbot.Api.Domain.Repositories; using MediatR; +using NDB.Application.DataContracts; using System; using System.Threading; using System.Threading.Tasks; diff --git a/Chatbot.Api.Application/Queries/Query.cs b/Chatbot.Api.Application/Queries/Query.cs deleted file mode 100644 index e712ec3..0000000 --- a/Chatbot.Api.Application/Queries/Query.cs +++ /dev/null @@ -1,8 +0,0 @@ -using MediatR; - -namespace Chatbot.Api.Application.Queries -{ - public abstract class Query : IRequest, IBaseRequest - { - } -} diff --git a/Chatbot.Api/Chatbot.Api.csproj b/Chatbot.Api/Chatbot.Api.csproj index b904b3b..df1745d 100644 --- a/Chatbot.Api/Chatbot.Api.csproj +++ b/Chatbot.Api/Chatbot.Api.csproj @@ -12,6 +12,7 @@ + diff --git a/Chatbot.Api/Startup.cs b/Chatbot.Api/Startup.cs index 2c59b38..bcd35ee 100644 --- a/Chatbot.Api/Startup.cs +++ b/Chatbot.Api/Startup.cs @@ -1,7 +1,6 @@ using AutoMapper; using Chatbot.Api.Authentication; using Chatbot.Api.Domain.Data; -using Chatbot.Api.Swagger; using Chatbot.Application; using MediatR; using MediatR.Pipeline; @@ -11,6 +10,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using NDB.Extensions.Swagger; using Newtonsoft.Json; using System.Reflection; @@ -45,7 +45,7 @@ namespace Chatbot.Api typeof(Application.Mappings.MappingProfile).Assembly); // Swagger - services.AddSwagger(); + services.AddSwagger("Chatbot API"); // Application services.AddApplicationServices(); @@ -75,7 +75,7 @@ namespace Chatbot.Api { endpoints.MapControllers(); }); - app.ConfigureSwagger(); + app.ConfigureSwagger("Chatbot API"); } private Assembly[] GetMediatRAssemblies() diff --git a/Chatbot.Api/Swagger/DtoSchemaFilter.cs b/Chatbot.Api/Swagger/DtoSchemaFilter.cs deleted file mode 100644 index 9dad03b..0000000 --- a/Chatbot.Api/Swagger/DtoSchemaFilter.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Chatbot.Api.Application.Commands; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Linq; - -namespace Chatbot.Api.Swagger -{ - public class DtoSchemaFilter : ISchemaFilter - { - public void Apply(OpenApiSchema schema, SchemaFilterContext context) - { - var targetType = context.Type; - while (targetType != null) - { - if (typeof(ICommand).IsAssignableFrom(targetType)) - { - foreach (var property in schema.Properties.ToList()) - { - property.Value.ReadOnly = false; - - switch (property.Key) - { - case "metadata": - schema.Properties.Remove(property.Key); - break; - default: - break; - } - } - } - - targetType = targetType.DeclaringType; - } - } - } -} diff --git a/Chatbot.Api/Swagger/PathParamsOperationFilter.cs b/Chatbot.Api/Swagger/PathParamsOperationFilter.cs deleted file mode 100644 index df43634..0000000 --- a/Chatbot.Api/Swagger/PathParamsOperationFilter.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System; -using System.Linq; -using System.Text.RegularExpressions; - -namespace Chatbot.Api.Swagger -{ - public class PathParamsOperationFilter : IOperationFilter - { - public void Apply(OpenApiOperation operation, OperationFilterContext context) - { - const string paramCaptureGroup = "param"; - - var openApiPathParameters = operation.Parameters.Where(param => param.In == ParameterLocation.Path).ToList(); - var pathParamRegEx = $@"\{{(?<{paramCaptureGroup}>[^\}}]+)\}}"; - - if (openApiPathParameters.Any()) - { - var pathParameterMatches = Regex.Matches(context.ApiDescription.RelativePath, pathParamRegEx, RegexOptions.Compiled); - var pathParameters = pathParameterMatches.Select(x => x.Groups[paramCaptureGroup].Value); - - foreach (var openApiPathParameter in openApiPathParameters) - { - var correspondingPathParameter = pathParameters.FirstOrDefault(x => - string.Equals(x, openApiPathParameter.Name, StringComparison.InvariantCultureIgnoreCase)); - - if (correspondingPathParameter != null) - openApiPathParameter.Name = correspondingPathParameter; - } - } - } - } -} diff --git a/Chatbot.Api/Swagger/SwaggerExtensions.cs b/Chatbot.Api/Swagger/SwaggerExtensions.cs deleted file mode 100644 index d9c5ed1..0000000 --- a/Chatbot.Api/Swagger/SwaggerExtensions.cs +++ /dev/null @@ -1,114 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; -using System.Collections.Generic; -using System.Linq; - -namespace Chatbot.Api.Swagger -{ - public static class SwaggerExtensions - { - public static IServiceCollection AddSwagger(this IServiceCollection services) - { - services.AddSwaggerGen(c => - { - c.SwaggerDoc("v1", - new OpenApiInfo - { - Title = "Chatbot API", - Version = "v1" - }); - - c.AddSecurityDefinition("Basic", - new OpenApiSecurityScheme - { - In = ParameterLocation.Header, - Description = @"JWT Authorization header using the Basic scheme. Enter 'Basic' [space] and then your token in the text input below. Example: 'Basic 12345abcdef'", - Name = "Authorization", - Scheme = "Basic", - Type = SecuritySchemeType.ApiKey - }); - - c.AddSecurityRequirement(new OpenApiSecurityRequirement() - { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference - { - Type = ReferenceType.SecurityScheme, - Id = "Basic" - }, - Scheme = "Basic", - Name = "Authorization", - In = ParameterLocation.Header - }, - new List() - } - }); - - c.OperationFilter(); - c.SchemaFilter(); - c.CustomSchemaIds(type => type.ToString()); - }); - - return services; - } - - public static IApplicationBuilder ConfigureSwagger(this IApplicationBuilder applicationBuilder) - { - applicationBuilder.UseSwagger(c => - { - c.PreSerializeFilters.Add((swagger, httpRequest) => - { - var (host, basePath, scheme) = GetUrlComponents(httpRequest); - - swagger.Servers = new List - { - new OpenApiServer {Url = $"{scheme}://{host}{basePath}"} - }; - }); - c.RouteTemplate = "swagger/{documentName}/swagger.json"; - }); - - applicationBuilder.UseSwaggerUI(c => - { - c.SwaggerEndpoint("v1/swagger.json", "Chatbot API"); - c.RoutePrefix = $"swagger"; - }); - - return applicationBuilder; - } - - private static (string host, string basePath, string scheme) GetUrlComponents(HttpRequest request) - { - var host = ExtractHost(request); - var basePath = ExtractBasePath(request); - var scheme = ExtractScheme(request); - - return (host, basePath, scheme); - } - - private static string ExtractHost(HttpRequest request) - { - if (request.Headers.ContainsKey("X-Forwarded-Host")) - return request.Headers["X-Forwarded-Host"].First(); - - return request.Host.Value; - } - - private static string ExtractBasePath(HttpRequest request) - { - if (request.Headers.ContainsKey("X-Forwarded-PathBase")) - return request.Headers["X-Forwarded-PathBase"].First(); - - return string.Empty; - } - - private static string ExtractScheme(HttpRequest request) - { - return request.Headers["X-Forwarded-Proto"].FirstOrDefault() ?? request.Scheme; - } - } -} \ No newline at end of file diff --git a/dependencies.props b/dependencies.props index b606756..6726903 100644 --- a/dependencies.props +++ b/dependencies.props @@ -10,5 +10,7 @@ 6.0.0 5.3.1 3.1.3 + 1.0.0 + 1.0.0 \ No newline at end of file