Browse Source

Merge pull request #17042 from masoodkhoshgard/dev

Human Friendly Enums in Swagger
pull/17130/head
maliming 3 years ago
committed by GitHub
parent
commit
0ba3652799
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs
  2. 23
      framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs

5
framework/src/Volo.Abp.Swashbuckle/Microsoft/Extensions/DependencyInjection/AbpSwaggerGenOptionsExtensions.cs

@ -9,4 +9,9 @@ public static class AbpSwaggerGenOptionsExtensions
{
swaggerGenOptions.DocumentFilter<AbpSwashbuckleDocumentFilter>();
}
public static void UserFriendlyEnums(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaFilter<AbpSwashbuckleEnumSchemaFilter>();
}
}

23
framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleEnumSchemaFilter.cs

@ -0,0 +1,23 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Linq;
namespace Volo.Abp.Swashbuckle;
public class AbpSwashbuckleEnumSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type.IsEnum)
{
schema.Enum.Clear();
schema.Type = nameof(String);
schema.Format = nameof(String);
Enum.GetNames(context.Type)
.ToList()
.ForEach(name => schema.Enum.Add(new OpenApiString($"{name}")));
}
}
}
Loading…
Cancel
Save