Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

API - Fix schema for enum types (add schema filter to swagger)

KrzysztofPajakf362ab78419a
diff --git a/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs b/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs
new file mode 100644
index 000000000..1b12e2c1a
--- /dev/null
+++ b/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs
@@ -0,0 +1,23 @@
+using Microsoft.OpenApi.Any;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using System;
+using System.Linq;
+
+namespace Grand.Api.Infrastructure
+{
+ public class EnumSchemaFilter : ISchemaFilter
+ {
+ public void Apply(OpenApiSchema model, SchemaFilterContext context)
+ {
+ if (context.Type.IsEnum)
+ {
+ model.Enum.Clear();
+ Enum.GetNames(context.Type)
+ .ToList()
+ .ForEach(n => model.Enum.Add(new OpenApiString(n)));
+ }
+ }
+ }
+}
+
diff --git a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
index b2ebc447d..a612dc819 100644
--- a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
+++ b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
@@ -56,6 +56,7 @@ namespace Grand.Api.Infrastructure
});
c.OperationFilter<AddParamOperationFilter>();
c.EnableAnnotations();
+ c.SchemaFilter<EnumSchemaFilter>();
});
}
}