From bf98792f96b261933b642ee710a034a41209868e Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 14 Jul 2026 18:59:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E5=85=B8=E8=AF=B7=E6=B1=82=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SimApiExtensions.cs | 1 + SwaggerFilters/DictionarySchemaFilter.cs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 SwaggerFilters/DictionarySchemaFilter.cs diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 70613e7..9a220dd 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -209,6 +209,7 @@ public static class SimApiExtensions x.OperationFilter(); x.OperationFilter(); x.SchemaFilter(); + x.SchemaFilter(); x.DocumentFilter(); if (simApiOptions.EnableSimApiAuth) { diff --git a/SwaggerFilters/DictionarySchemaFilter.cs b/SwaggerFilters/DictionarySchemaFilter.cs new file mode 100644 index 0000000..a735a52 --- /dev/null +++ b/SwaggerFilters/DictionarySchemaFilter.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using Microsoft.OpenApi; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace SimApi.SwaggerFilters; + +public class DictionarySchemaFilter : ISchemaFilter +{ + public void Apply(IOpenApiSchema schema, SchemaFilterContext context) + { + if (!context.Type.IsGenericType || context.Type.GetGenericTypeDefinition() != typeof(Dictionary<,>)) + return; + + if (schema is not OpenApiSchema concrete) return; + + var valueType = context.Type.GetGenericArguments()[1]; + concrete.Type = JsonSchemaType.Object; + concrete.AdditionalPropertiesAllowed = true; + concrete.AdditionalProperties = context.SchemaGenerator.GenerateSchema(valueType, context.SchemaRepository); + concrete.Properties?.Clear(); + concrete.Example = null; + concrete.Examples?.Clear(); + } +} \ No newline at end of file