Open Closed

Enum descriptions in swagger #6869


User avatar
0
naeem76 created
  • ABP Framework version: v7.1.0
  • UI Type: MVC
  • Database System: EF Core / MySQL
  • Tiered (for MVC) or Auth Server Separated (for Angular): No
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Currently trying to describe enum models in Swagger, don't want to change the API parameters, they should still use and return integers, but the description should show which integer means what.

I've tried this

options.SchemaFilter<EnumSchemaFilter>();

public class EnumSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        if (context.Type.IsEnum)
        {
            var array = new OpenApiArray();
            array.AddRange(Enum.GetNames(context.Type).Select(n => new OpenApiString(n)));
            // NSwag
            schema.Extensions.Add("x-enumNames", array);
            // Openapi-generator
            schema.Extensions.Add("x-enum-varnames", array);
        }
    }
}

Also tried using the package Unchase.Swashbuckle.AspNetCore.Extensions

options.AddEnumsWithValuesFixFilters();

But results are the same, no change

Enum models are still


2 Answer(s)
  • User Avatar
    1
    liangshiwei created
    Support Team Fullstack Developer

    It works for me:

  • User Avatar
    0
    naeem76 created

    That at least helped me steer in the right direction. Just learned that the order of that function matters, I had it at the end, we have a couple of other settings, but when I put it right after the line options.CustomSchemaIds(type => type.FullName);, it worked.

    Thank you!

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11