last ...
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xCommons.Configurations {
|
||||
public partial class XSwaggerConfiguration {
|
||||
public string Title { get; set; } = "";
|
||||
@@ -7,6 +9,7 @@ namespace xCommons.Configurations {
|
||||
|
||||
public XSwaggerContact Contact { get; set; } = null;
|
||||
public XSwaggerLicence License { get; set; } = null;
|
||||
public Dictionary<string, string> DefaultValues {get; set;} = null;
|
||||
}
|
||||
|
||||
public partial class XSwaggerContact {
|
||||
@@ -19,4 +22,9 @@ namespace xCommons.Configurations {
|
||||
public string Name { get; set; }
|
||||
public string Url { get; set; }
|
||||
}
|
||||
|
||||
public partial class XSwaggerDefaultValues
|
||||
{
|
||||
public Dictionary<string, string> Values { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -45,11 +45,13 @@ namespace xCommons.Extensions {
|
||||
/// <param name="xmlFilePath"></param>
|
||||
/// <param name="addRequiredXPoweredFilter"></param>
|
||||
/// <param name="addXTokenAuthorization"></param>
|
||||
/// <param name="addDefaultValueFilter"></param>
|
||||
public static void AddXSwaggerGenOptions (
|
||||
this SwaggerGenOptions source,
|
||||
string xmlFilePath = null,
|
||||
bool addRequiredXPoweredFilter = true,
|
||||
bool addXTokenAuthorization = true
|
||||
bool addXTokenAuthorization = true,
|
||||
bool addDefaultValueFilter = true
|
||||
) {
|
||||
//
|
||||
if (source.IsNull ()) {
|
||||
@@ -68,6 +70,12 @@ namespace xCommons.Extensions {
|
||||
source.OperationFilter<RequireXPoweredOperationFilter> ();
|
||||
}
|
||||
|
||||
//
|
||||
// Add Default Values Filter ...
|
||||
if (addDefaultValueFilter) {
|
||||
source.OperationFilter<SwaggerDefaultValuesOperationFilter> ();
|
||||
}
|
||||
|
||||
//
|
||||
// Handle XToken Authorizations ...
|
||||
if (addXTokenAuthorization) {
|
||||
@@ -151,13 +159,15 @@ namespace xCommons.Extensions {
|
||||
/// <param name="xmlFilePath"></param>
|
||||
/// <param name="addRequiredXPoweredFilter"></param>
|
||||
/// <param name="addXTokenAuthorization"></param>
|
||||
/// <param name="addDefaultValueFilter"></param>
|
||||
public static void AddXSwagger (
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration,
|
||||
SwaggerGenOptions settings = null,
|
||||
string xmlFilePath = null,
|
||||
bool addRequiredXPoweredFilter = true,
|
||||
bool addXTokenAuthorization = true
|
||||
bool addXTokenAuthorization = true,
|
||||
bool addDefaultValueFilter = true
|
||||
) {
|
||||
//
|
||||
var xSwaggerConfig = configuration.GetXSwaggerConfiguration ();
|
||||
@@ -165,6 +175,9 @@ namespace xCommons.Extensions {
|
||||
xSwaggerConfig = new XSwaggerConfiguration ();
|
||||
}
|
||||
|
||||
//
|
||||
source.AddSingleton(xSwaggerConfig);
|
||||
|
||||
//
|
||||
#region Prepare SwaggerGenOptions ...
|
||||
if (settings.IsNull ()) {
|
||||
@@ -172,8 +185,9 @@ namespace xCommons.Extensions {
|
||||
}
|
||||
settings.AddXSwaggerGenOptions (
|
||||
xmlFilePath: xmlFilePath,
|
||||
addRequiredXPoweredFilter: addRequiredXPoweredFilter,
|
||||
addXTokenAuthorization: addXTokenAuthorization
|
||||
addDefaultValueFilter: addDefaultValueFilter,
|
||||
addXTokenAuthorization: addXTokenAuthorization,
|
||||
addRequiredXPoweredFilter: addRequiredXPoweredFilter
|
||||
);
|
||||
#endregion
|
||||
|
||||
@@ -242,6 +256,9 @@ namespace xCommons.Extensions {
|
||||
opt.SwaggerDoc ("v1", apiDoc);
|
||||
}
|
||||
);
|
||||
|
||||
//
|
||||
// Register Swagger Operation Filters ...
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Linq;
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xCommons.Filters
|
||||
{
|
||||
public class SwaggerDefaultValuesOperationFilter : IOperationFilter
|
||||
{
|
||||
private readonly XSwaggerConfiguration config;
|
||||
|
||||
public SwaggerDefaultValuesOperationFilter(
|
||||
XSwaggerConfiguration config
|
||||
)
|
||||
{
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void Apply(
|
||||
OpenApiOperation operation,
|
||||
OperationFilterContext context
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Configuration ...
|
||||
if (
|
||||
!config.IsNull() &&
|
||||
!config.DefaultValues.IsNull() &&
|
||||
config.DefaultValues.HasChild()
|
||||
)
|
||||
{
|
||||
//
|
||||
foreach (var prv in config.DefaultValues)
|
||||
{
|
||||
//
|
||||
// Detect Parameter ...
|
||||
var swaggerParam = operation.Parameters.FirstOrDefault(p => p.Name == prv.Key);
|
||||
if (!swaggerParam.IsNull())
|
||||
{
|
||||
swaggerParam.Schema.Default = new OpenApiString(prv.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<!-- <add key="megan" value="https://hub.megan.ir/nuget/index.json" /> -->
|
||||
<add key="nuget" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
|
||||
<add key="liget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" />
|
||||
<add key="baget" value="https://nuget.saherelmhub.ir/v3/index.json" protocolVersion="3" disableTLSCertificateValidation="true" />
|
||||
</packageSources>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user