From 39452b51ff77ac0b79da3879b55c3cc766aaf735 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 22 May 2026 20:26:06 +0330 Subject: [PATCH] last ... --- Configurations/XSwaggerConfiguration.cs | 8 ++++ Extensions/DIExtensions.cs | 25 ++++++++-- .../SwaggerDefaultValuesOperationFilter.cs | 48 +++++++++++++++++++ nuget.config | 3 +- 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 Filters/SwaggerDefaultValuesOperationFilter.cs diff --git a/Configurations/XSwaggerConfiguration.cs b/Configurations/XSwaggerConfiguration.cs index f5ed5cf..98330a9 100644 --- a/Configurations/XSwaggerConfiguration.cs +++ b/Configurations/XSwaggerConfiguration.cs @@ -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 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 Values { get; set; } + } } \ No newline at end of file diff --git a/Extensions/DIExtensions.cs b/Extensions/DIExtensions.cs index 0a82265..b5c7530 100644 --- a/Extensions/DIExtensions.cs +++ b/Extensions/DIExtensions.cs @@ -45,11 +45,13 @@ namespace xCommons.Extensions { /// /// /// + /// 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 (); } + // + // Add Default Values Filter ... + if (addDefaultValueFilter) { + source.OperationFilter (); + } + // // Handle XToken Authorizations ... if (addXTokenAuthorization) { @@ -151,13 +159,15 @@ namespace xCommons.Extensions { /// /// /// + /// 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 ... } /// diff --git a/Filters/SwaggerDefaultValuesOperationFilter.cs b/Filters/SwaggerDefaultValuesOperationFilter.cs new file mode 100644 index 0000000..507a6f3 --- /dev/null +++ b/Filters/SwaggerDefaultValuesOperationFilter.cs @@ -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); + } + } + } + } + } +} \ No newline at end of file diff --git a/nuget.config b/nuget.config index 87b6eb0..c6fd40c 100644 --- a/nuget.config +++ b/nuget.config @@ -1,7 +1,8 @@ + - + \ No newline at end of file