1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01: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:
Oscar Hinton 2024-02-09 09:47:03 +01:00 committed by GitHub
parent b6255a64fe
commit 0766806279
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 6 deletions

View File

@ -5,6 +5,7 @@ using Bit.Core.IdentityServer;
using Bit.Core.Settings; using Bit.Core.Settings;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Bit.SharedWeb.Health; using Bit.SharedWeb.Health;
using Bit.SharedWeb.Swagger;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
@ -69,6 +70,8 @@ public static class ServiceCollectionExtensions
config.DescribeAllParametersInCamelCase(); config.DescribeAllParametersInCamelCase();
// config.UseReferencedDefinitionsForEnums(); // config.UseReferencedDefinitionsForEnums();
config.SchemaFilter<EnumSchemaFilter>();
var apiFilePath = Path.Combine(AppContext.BaseDirectory, "Api.xml"); var apiFilePath = Path.Combine(AppContext.BaseDirectory, "Api.xml");
config.IncludeXmlComments(apiFilePath, true); config.IncludeXmlComments(apiFilePath, true);
var coreFilePath = Path.Combine(AppContext.BaseDirectory, "Core.xml"); var coreFilePath = Path.Combine(AppContext.BaseDirectory, "Core.xml");

View File

@ -14,7 +14,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="MessagePack" Version="2.5.140" /> <PackageReference Include="MessagePack" Version="2.5.140" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -9,6 +9,7 @@ using Bit.Core.SecretsManager.Repositories.Noop;
using Bit.Core.Settings; using Bit.Core.Settings;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Bit.Identity.Utilities; using Bit.Identity.Utilities;
using Bit.SharedWeb.Swagger;
using Bit.SharedWeb.Utilities; using Bit.SharedWeb.Utilities;
using Duende.IdentityServer.Extensions; using Duende.IdentityServer.Extensions;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
@ -64,6 +65,7 @@ public class Startup
services.AddSwaggerGen(c => services.AddSwaggerGen(c =>
{ {
c.SchemaFilter<EnumSchemaFilter>();
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Bitwarden Identity", Version = "v1" }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "Bitwarden Identity", Version = "v1" });
}); });

View File

@ -6,4 +6,8 @@
<ProjectReference Include="..\Infrastructure.EntityFramework\Infrastructure.EntityFramework.csproj" /> <ProjectReference Include="..\Infrastructure.EntityFramework\Infrastructure.EntityFramework.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
</ItemGroup>
</Project> </Project>

View 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);
}
}
}