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