From 380353c79459b2f8d19d370b46486c3212e653d5 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 1 Jul 2024 14:19:52 -0500 Subject: [PATCH] refactor: Get language options from the constructor to avoid memory reallocations --- src/HostApplication/Extensions/SwaggerGen.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/HostApplication/Extensions/SwaggerGen.cs b/src/HostApplication/Extensions/SwaggerGen.cs index dcaa358b..663b9389 100644 --- a/src/HostApplication/Extensions/SwaggerGen.cs +++ b/src/HostApplication/Extensions/SwaggerGen.cs @@ -48,15 +48,21 @@ public static IServiceCollection AddSwagger(this IServiceCollection services) private class AcceptLanguageHeaderParameter(IConfiguration configuration) : IOperationFilter { - public void Apply(OpenApiOperation operation, OperationFilterContext context) + private readonly IList _languageOptions = CreateLanguageOptions(configuration); + private readonly string _defaultLanguage = configuration.GetDefaultLanguage(); + + private static List CreateLanguageOptions(IConfiguration configuration) { - operation.Parameters ??= []; var languageOptions = new List(); var languages = configuration.GetLanguages(); - var defaultLanguage = configuration.GetDefaultLanguage(); - foreach (var language in languages) + foreach (var language in languages) languageOptions.Add(new OpenApiString(language)); + return languageOptions; + } + public void Apply(OpenApiOperation operation, OperationFilterContext context) + { + operation.Parameters ??= []; operation.Parameters.Add(new OpenApiParameter { Name = "Accept-Language", @@ -66,8 +72,8 @@ public void Apply(OpenApiOperation operation, OperationFilterContext context) Schema = new OpenApiSchema { Type = "string", - Default = new OpenApiString(defaultLanguage), - Enum = languageOptions + Default = new OpenApiString(_defaultLanguage), + Enum = _languageOptions } }); }