Base solution for your next web application
Open Closed

Swagger error with 2 enum in same class #10964


User avatar
0
andmattia created
  • What is your product version? 9.2
  • What is your product type (Angular or MVC)? angular
  • What is product framework type (.net framework or .net core)? .NetCore

If issue related with ABP Framework

  • What is ABP Framework version? 5.13

I migrate from ABP 4.8.1 -> 5.13 and I found an issue on Swagger

 if (WebConsts.SwaggerUiEnabled)
            {
                //Swagger - Enable this line and the related lines in Configure method to enable swagger UI
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new OpenApiInfo() { Title = " API", Version = "v1" });
                    options.DocInclusionPredicate((docName, description) => true);
                    options.ParameterFilter<SwaggerEnumParameterFilter>();
                    options.SchemaFilter<SwaggerEnumSchemaFilter>();
                    options.OperationFilter<SwaggerOperationIdFilter>();
                    //options.OperationFilter<SwaggerOperationFilter>();
                    options.CustomDefaultSchemaIdSelector();
                    options.CustomSchemaIds(o => o.FullName);
                    options.OrderActionsBy(o => o.RelativePath);
                }).AddSwaggerGenNewtonsoftSupport();
            }

my DTO class is

public class InputDto: ...
    {
        public string FilterText { get; set; }
        public long? Id { get; set; }
        ...
        public OrderStatus? Status { get; set; }
        ...
        public OrderStatus[] StatusList { get; set; }
        ...
    }

The only way to fix is add a try catch

private static void AddEnumSpec(Type type, ParameterFilterContext context)
        {
            var schema = context.SchemaRepository.Schemas.GetOrAdd($"#/definitions/{type.Name}", () =>
                context.SchemaGenerator.GenerateSchema(type, context.SchemaRepository)
            );

            if (schema.Reference == null || !type.IsEnum)
            {
                return;
            }

            var enumNames = new OpenApiArray();
            enumNames.AddRange(Enum.GetNames(type).Select(_ => new OpenApiString(_)));
            try
            {
                schema.Extensions.Add("x-enumNames", enumNames);
            }
            catch (Exception ex)
            {
                Console.Write("Ingore this error");
            }
        }

3 Answer(s)