init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
142
Kreta.WebApi/Naplo/Kreta.Naplo.WebApi/App_Start/SwaggerConfig.cs
Normal file
142
Kreta.WebApi/Naplo/Kreta.Naplo.WebApi/App_Start/SwaggerConfig.cs
Normal file
|
@ -0,0 +1,142 @@
|
|||
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.Naplo.Dto.V3.Attributes;
|
||||
using Kreta.Naplo.WebApi;
|
||||
using Swashbuckle.Application;
|
||||
using Swashbuckle.Examples;
|
||||
using Swashbuckle.Swagger;
|
||||
using WebActivatorEx;
|
||||
|
||||
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), nameof(SwaggerConfig.Register))]
|
||||
namespace Kreta.Naplo.WebApi
|
||||
{
|
||||
public class SwaggerConfig
|
||||
{
|
||||
private static readonly string Header = "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"]))
|
||||
{
|
||||
var thisAssembly = typeof(SwaggerConfig).Assembly;
|
||||
GlobalConfiguration.Configuration
|
||||
.EnableSwagger(c =>
|
||||
{
|
||||
c.MultipleApiVersions(
|
||||
(apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
|
||||
(vc) =>
|
||||
{
|
||||
_ = vc.Version(V3.Constants.RoutePrefix, $"Kreta.Naplo.WebApi {V3.Constants.RoutePrefix}");
|
||||
_ = vc.Version(V2.VersionedRouteInfo.RoutePrefix, $"Kreta.Naplo.WebApi {V2.VersionedRouteInfo.RoutePrefix}");
|
||||
}
|
||||
);
|
||||
|
||||
c.IncludeXmlComments(string.Format(@"{0}\App_Data\Kreta.Naplo.WebApi.XML", AppDomain.CurrentDomain.BaseDirectory));
|
||||
c.IncludeXmlComments(string.Format(@"{0}\App_Data\Kreta.Naplo.Dto.XML", AppDomain.CurrentDomain.BaseDirectory));
|
||||
c.OperationFilter<ExamplesOperationFilter>();
|
||||
c.OperationFilter<DescriptionOperationFilter>();
|
||||
c.UseFullTypeNameInSchemaIds();
|
||||
|
||||
_ = c.ApiKey("Token")
|
||||
.Description("Filling bearer token here")
|
||||
.Name("Authorization").Name("ApiKey")
|
||||
.In(Header);
|
||||
|
||||
c.OperationFilter(() => new AddAuthorizationHeaderParameter());
|
||||
c.SchemaFilter<SwaggerChangeTypeFilter>();
|
||||
c.RootUrl(ResolveBasePath);
|
||||
})
|
||||
.EnableSwaggerUi(c =>
|
||||
{
|
||||
c.InjectStylesheet(thisAssembly, "Kreta.Naplo.WebApi.Content.naplo-swagger-ui-custom.css");
|
||||
c.DocExpansion(DocExpansion.List);
|
||||
c.EnableApiKeySupport("Authorization", "header");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 class AddAuthorizationHeaderParameter : IOperationFilter
|
||||
{
|
||||
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
|
||||
{
|
||||
if (operation.parameters == null)
|
||||
{
|
||||
operation.parameters = new List<Parameter>();
|
||||
}
|
||||
operation.parameters.Add(new Parameter
|
||||
{
|
||||
name = "Authorization",
|
||||
@in = Header,
|
||||
description = "access token",
|
||||
required = false,
|
||||
type = "string"
|
||||
});
|
||||
if (ApiKeyConfiguration.Instance.Enabled)
|
||||
{
|
||||
operation.parameters.Add(new Parameter
|
||||
{
|
||||
name = "ApiKey",
|
||||
@in = Header,
|
||||
description = "Header-ben levő Api kulcs védelem",
|
||||
required = false,
|
||||
type = "string",
|
||||
@default = ApiKeyConfiguration.Instance.ApiKey,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue