mirror of
https://github.com/bitwarden/server
synced 2026-01-06 10:34:01 +00:00
[PM-6165] Add x-enum-varnames to improve swagger generation (#3767)
Improves code generation of enums for the server bindings in the sdk. Bindings will now use the appropiate variable name from the server. Works by adding a filter which appends x-enum-varnames to enums with the name from c#.
This commit is contained in:
26
src/SharedWeb/Swagger/EnumSchemaFilter.cs
Normal file
26
src/SharedWeb/Swagger/EnumSchemaFilter.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace Bit.SharedWeb.Swagger;
|
||||
|
||||
/// <summary>
|
||||
/// Adds x-enum-varnames containing the name of enums. Useful for code generation.
|
||||
///</summary>
|
||||
/// <remarks>
|
||||
/// Ideally we would use `oneOf` instead but it's not currently handled well by our swagger generator.
|
||||
///
|
||||
/// Credits: https://github.com/domaindrivendev/Swashbuckle.WebApi/issues/1287#issuecomment-655164215
|
||||
/// </remarks>
|
||||
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)));
|
||||
schema.Extensions.Add("x-enum-varnames", array);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user