142 lines
5.8 KiB
C#
142 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Web.Http;
|
|
using System.Web.Http.Description;
|
|
using Kreta.Core.Configuratiaton;
|
|
using Kreta.Core.Enum;
|
|
using Kreta.Core.Exceptions;
|
|
using Kreta.Ellenorzo.Dto.VN.Attributes;
|
|
using Kreta.Ellenorzo.WebApi;
|
|
using Swashbuckle.Application;
|
|
using Swashbuckle.Examples;
|
|
using Swashbuckle.Swagger;
|
|
using WebActivatorEx;
|
|
|
|
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), nameof(SwaggerConfig.Register))]
|
|
|
|
namespace Kreta.Ellenorzo.WebApi
|
|
{
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1053:StaticHolderTypesShouldNotHaveConstructors")]
|
|
public class SwaggerConfig
|
|
{
|
|
private const string HeaderString = "header";
|
|
|
|
public class SwaggerChangeTypeFilter : ISchemaFilter
|
|
{
|
|
public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
|
|
{
|
|
if (schema?.properties == null || type == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var selectedProperties = type.GetProperties().Where(t => t.GetCustomAttribute<SwaggerChangeTypeAttribute>() != null);
|
|
|
|
foreach (var property in selectedProperties)
|
|
{
|
|
var x = schema.properties[property.Name];
|
|
if (property.GetCustomAttribute<SwaggerChangeTypeAttribute>().NewType == typeof(string))
|
|
{
|
|
x.@ref = null;
|
|
x.type = "string";
|
|
}
|
|
else
|
|
{
|
|
throw new BlException(BlExceptionType.ElvartErtekNemTalalhato);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Register()
|
|
{
|
|
if (bool.Parse(ConfigurationManager.AppSettings["IsSwaggerUiEnabled"]))
|
|
{
|
|
GlobalConfiguration.Configuration
|
|
.EnableSwagger(c =>
|
|
{
|
|
c.MultipleApiVersions(
|
|
(apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
|
|
(vc) =>
|
|
{
|
|
_ = vc.Version(VN.Constants.RoutePrefix, $"Kreta.Ellenorzo.WebApi {VN.Constants.RoutePrefix}");
|
|
}
|
|
);
|
|
|
|
c.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}\\App_Data\\Kreta.Ellenorzo.Web.XML");
|
|
c.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}\\App_Data\\Kreta.Ellenorzo.Dto.xml");
|
|
c.OperationFilter<ExamplesOperationFilter>();
|
|
c.OperationFilter<DescriptionOperationFilter>();
|
|
c.UseFullTypeNameInSchemaIds();
|
|
|
|
_ = c.ApiKey("Token")
|
|
.Description("Filling bearer token here")
|
|
.Name("Authorization")
|
|
.In(HeaderString);
|
|
|
|
c.OperationFilter<AddAuthorizationHeaderParameter>();
|
|
c.SchemaFilter<SwaggerChangeTypeFilter>();
|
|
c.RootUrl(ResolveBasePath);
|
|
})
|
|
.EnableSwaggerUi(c =>
|
|
{
|
|
c.DocExpansion(DocExpansion.List);
|
|
c.EnableApiKeySupport("Authorization", HeaderString);
|
|
});
|
|
}
|
|
}
|
|
|
|
private static string ResolveBasePath(HttpRequestMessage message)
|
|
{
|
|
var virtualPathRoot = message.GetRequestContext().VirtualPathRoot;
|
|
var schemeAndHost = "https://" + message.RequestUri.Host;
|
|
return new Uri(new Uri(schemeAndHost, UriKind.Absolute), virtualPathRoot).AbsoluteUri;
|
|
}
|
|
|
|
private static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
|
|
{
|
|
string[] path = apiDesc.RelativePath.Split(new[] { '/' });
|
|
string pathVersion = path[0];
|
|
|
|
return CultureInfo.InvariantCulture.CompareInfo.IndexOf(pathVersion, targetApiVersion, CompareOptions.IgnoreCase) >= 0;
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
|
|
private sealed class AddAuthorizationHeaderParameter : IOperationFilter
|
|
{
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
|
|
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
|
|
{
|
|
operation.parameters = operation.parameters ?? new List<Parameter>();
|
|
|
|
operation.parameters.Add(new Parameter
|
|
{
|
|
name = "Authorization",
|
|
@in = HeaderString,
|
|
description = "access token",
|
|
required = false,
|
|
type = "string"
|
|
});
|
|
|
|
if (ApiKeyConfiguration.Instance.Enabled)
|
|
{
|
|
operation.parameters.Add(new Parameter
|
|
{
|
|
name = "ApiKey",
|
|
@in = HeaderString,
|
|
description = "api key",
|
|
required = false,
|
|
type = "string",
|
|
@default = ApiKeyConfiguration.Instance.ApiKey
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|