Skip to content

Commit bf98792

Browse files
committed
字典请求返回格式化
1 parent 8c5081d commit bf98792

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

SimApiExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ string GetSimpleTypeName(Type t, int depth = 0)
209209
x.OperationFilter<SimApiSignOperationFilter>();
210210
x.OperationFilter<AesBodyOperationFilter>();
211211
x.SchemaFilter<GlobalDynamicObjectSchemaFilter>();
212+
x.SchemaFilter<DictionarySchemaFilter>();
212213
x.DocumentFilter<RemoveEmptyTagsFilter>();
213214
if (simApiOptions.EnableSimApiAuth)
214215
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using Microsoft.OpenApi;
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace SimApi.SwaggerFilters;
6+
7+
public class DictionarySchemaFilter : ISchemaFilter
8+
{
9+
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
10+
{
11+
if (!context.Type.IsGenericType || context.Type.GetGenericTypeDefinition() != typeof(Dictionary<,>))
12+
return;
13+
14+
if (schema is not OpenApiSchema concrete) return;
15+
16+
var valueType = context.Type.GetGenericArguments()[1];
17+
concrete.Type = JsonSchemaType.Object;
18+
concrete.AdditionalPropertiesAllowed = true;
19+
concrete.AdditionalProperties = context.SchemaGenerator.GenerateSchema(valueType, context.SchemaRepository);
20+
concrete.Properties?.Clear();
21+
concrete.Example = null;
22+
concrete.Examples?.Clear();
23+
}
24+
}

0 commit comments

Comments
 (0)