diff --git a/src/Api/Utilities/ServiceCollectionExtensions.cs b/src/Api/Utilities/ServiceCollectionExtensions.cs index 4c6664bf7d..310a775891 100644 --- a/src/Api/Utilities/ServiceCollectionExtensions.cs +++ b/src/Api/Utilities/ServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using Bit.Core.IdentityServer; using Bit.Core.Settings; using Bit.Core.Utilities; using Bit.SharedWeb.Health; +using Bit.SharedWeb.Swagger; using Microsoft.AspNetCore.Authorization; using Microsoft.OpenApi.Models; @@ -69,6 +70,8 @@ public static class ServiceCollectionExtensions config.DescribeAllParametersInCamelCase(); // config.UseReferencedDefinitionsForEnums(); + config.SchemaFilter(); + var apiFilePath = Path.Combine(AppContext.BaseDirectory, "Api.xml"); config.IncludeXmlComments(apiFilePath, true); var coreFilePath = Path.Combine(AppContext.BaseDirectory, "Core.xml"); diff --git a/src/Identity/Identity.csproj b/src/Identity/Identity.csproj index 2f40a30e27..b88cec5e3a 100644 --- a/src/Identity/Identity.csproj +++ b/src/Identity/Identity.csproj @@ -14,7 +14,6 @@ - diff --git a/src/Identity/Startup.cs b/src/Identity/Startup.cs index dda5fb03ee..7f699e70a7 100644 --- a/src/Identity/Startup.cs +++ b/src/Identity/Startup.cs @@ -9,6 +9,7 @@ using Bit.Core.SecretsManager.Repositories.Noop; using Bit.Core.Settings; using Bit.Core.Utilities; using Bit.Identity.Utilities; +using Bit.SharedWeb.Swagger; using Bit.SharedWeb.Utilities; using Duende.IdentityServer.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -64,6 +65,7 @@ public class Startup services.AddSwaggerGen(c => { + c.SchemaFilter(); c.SwaggerDoc("v1", new OpenApiInfo { Title = "Bitwarden Identity", Version = "v1" }); }); diff --git a/src/SharedWeb/SharedWeb.csproj b/src/SharedWeb/SharedWeb.csproj index 053c9ba0b1..e25cc988f1 100644 --- a/src/SharedWeb/SharedWeb.csproj +++ b/src/SharedWeb/SharedWeb.csproj @@ -1,9 +1,13 @@ - - - - - + + + + + + + + + diff --git a/src/SharedWeb/Swagger/EnumSchemaFilter.cs b/src/SharedWeb/Swagger/EnumSchemaFilter.cs new file mode 100644 index 0000000000..301fbfeca8 --- /dev/null +++ b/src/SharedWeb/Swagger/EnumSchemaFilter.cs @@ -0,0 +1,26 @@ +using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace Bit.SharedWeb.Swagger; + +/// +/// Adds x-enum-varnames containing the name of enums. Useful for code generation. +/// +/// +/// 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 +/// +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); + } + } +}